springmvc 实现文件上传

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();
}

 

上一篇:Java Fileupload


下一篇:springboot 跨域设置