springmvc 實現文件上傳
阿新 • • 發佈:2019-04-16
stp tid comm part 3.1 out upload stream gmv <version>1.3.1</version>
</dependency> int len=0;
while((len=is.read(buff))>0) {
fos.write(buff, 0, len);
}
fos.close();
is.close();
}
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>
</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);
while((len=is.read(buff))>0) {
fos.write(buff, 0, len);
}
fos.close();
is.close();
}
springmvc 實現文件上傳