上传--spring-boot

时间:2022-01-21 10:37:02

<dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3.1</version>
  </dependency>

@RequestMapping(value = "/upload/", method = RequestMethod.POST)
@ResponseBody
public String upload(
@RequestParam("file") MultipartFile upfile,
HttpServletRequest req) throws IOException { String path ="D:\\study\\";
String imageName =upfile.getOriginalFilename();
// |获取输入流
InputStream is = upfile.getInputStream();
// |文件输出流
//OutputStream os = new FileOutputStream(new File(path,upfile.getOriginalFilename()));
OutputStream os = new FileOutputStream(new File(path,imageName));
System.out.println("come0");
// |循环写入
int length = 0;
byte[] buffer = new byte[128];
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
System.out.println("come1");
is.close();
os.close();
System.out.println("come2"); return imageName;
}