Springboot中图片上传报java.io.FileNotFoundException: C:\Users\WIzarder\AppData\Local\Temp\tomcat.8080.589

Springboot图片上传中用MultipartFile接受文件,上传报错java.io.FileNotFoundException: C:\Users\WIzarder\AppData\Local\Temp\tomcat.8080.5893235156551690444\
原因:MultipartFile在调用方法transferTo把文件上传到指定的路径后,会自动关闭这个流,后面无法再调用这个方法。因此需要用异步方法处理一下
解决:用异步方法来上传可以解决

 1   @PostMapping("/upload")
 2     public String upload(@RequestParam("email") String email,
 3                          @RequestParam("password") String password,
 4                          @RequestPart("headimg")MultipartFile multipartFile,
 5                          @RequestPart("lifeimg")MultipartFile[] multipartFiles) throws IOException {
 6        int length = multipartFiles.length;
 7         log.info("email={},password={},headImg={},lifeImg{}",email,password,multipartFile.getSize(),
 8                 multipartFiles.length);
 9         uoloadImg(multipartFile,multipartFiles);//用异步方法来上传完美解决
10 
11         return "index";
12     }
13     @Async
14     public void  uoloadImg(MultipartFile head,MultipartFile[] photos) throws IOException {
15         if (photos.length > 0){
16             for (MultipartFile file : photos) {
17                 if (!file.isEmpty()){
18                     String originalFilename = file.getOriginalFilename();
19                     file.transferTo(new File("G:\\上传图片\\"+originalFilename));
20                 }
21 
22             }
23         }
24         if (!head.isEmpty()){
25             String originalFilename = head.getOriginalFilename();
26             head.transferTo(new File("G:\\上传图片\\" + originalFilename));
27         }
28     }

 

转载博客:(9条消息) Springboot2中文件上传报java.io.FileNotFoundException: C:\Users\WIzarder\AppData\Local\Temp\tomcat.8080.589_Wizarder1的博客-CSDN博客

posted @ 2022-04-12 18:48  谜语+  阅读(736)  评论(0编辑  收藏  举报