1. 程式人生 > >springmvc 實現文件上傳

springmvc 實現文件上傳

stp tid comm part 3.1 out upload stream gmv

1。添加jar包

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>

<version>1.3.1</version>
</dependency>

2.

@ResponseBody
@RequestMapping("upfile")
public void upfile(@RequestParam("file")MultipartFile req) throws Exception {
InputStream is=req.getInputStream();
byte[] buff=new byte[1024*4];
String filename=req.getOriginalFilename();
FileOutputStream fos=new FileOutputStream("d:\\"+filename);

int len=0;
while((len=is.read(buff))>0) {
fos.write(buff, 0, len);
}
fos.close();
is.close();
}

springmvc 實現文件上傳