Spring项目MultipartFile转File调用transferTo抛IOException问题求助
我正在尝试将MultipartFile转换为File,但执行multipartFile.transferTo(uploadedFile);时抛出了IOException。
我观察到应用是在'/tmp/tomcat.8080.434182389216770471/work/Tomcat/localhost/ROOT/upload_43f33560_23be_4189_843c_d9062cb44ec2_00000000.tmp'路径下查找uploadedFile,而非我指定的src/main/resources/targetFile.tmp路径。
@GetMapping("/reportError") public String error(Model model) throws MessagingException, IOException { EmailService emailService = new EmailService(); File uploadedFile = new File("src/main/resources/targetFile.tmp"); multipartFile.transferTo(uploadedFile); emailService.sendmail(Arrays.asList(uploadedFile, dailyReportFile, shiftReportFile)); model.addAttribute("msg", "Thanks for reporting.\nIt helps us improve the app."); return "messageView"; }
java.io.IOException: java.io.FileNotFoundException:
/tmp/tomcat.8080.434182389216770471/work/Tomcat/localhost/ROOT/upload_43f33560_23be_4189_843c_d9062cb44ec2_00000000.tmp
(No such file or directory) at
org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:122)
~[tomcat-embed-core-9.0.45.jar:9.0.45] at
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.transferTo(StandardMultipartHttpServletRequest.java:256)
~[spring-web-5.3.6.jar:5.3.6] at
gmailgdogra.controller.AppController.error(AppController.java:202)
~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) ~[na:1.8.0_292] at
java.lang.Thread.run(Thread.java:748) [na:1.8.0_292] Caused by:
java.io.FileNotFoundException:
/tmp/tomcat.8080.434182389216770471/work/Tomcat/localhost/ROOT/upload_43f33560_23be_4189_843c_d9062cb44ec2_00000000.tmp
(No such file or directory) at java.io.FileInputStream.open0(Native
Method) ~[na:1.8.0_292] at
java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_292]
at java.io.FileInputStream.(FileInputStream.java:138)
~[na:1.8.0_292] at
org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:404)
~[tomcat-embed-core-9.0.45.jar:9.0.45] at
org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:120)
~[tomcat-embed-core-9.0.45.jar:9.0.45] ... 52 common frames omitted
同类代码在测试环境中可正常运行
@Test void convert() throws IOException { MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes()); File file = new File("src/main/resources/targetFile.tmp"); multipartFile.transferTo(file); assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8")) .isEqualTo("Hello World"); }
如果我不发送转换后的Multipart文件,以下代码可以正常运行
@GetMapping("/reportError") public String error(Model model) throws MessagingException, IOException { EmailService emailService = new EmailService(); emailService.sendmail(Arrays.asList(dailyReportFile, shiftReportFile)); model.addAttribute("msg", "Thanks for reporting.\nIt helps us improve the app."); return "messageView"; }
@GetMapping("/reportError") public String error(Model model) throws MessagingException, IOException { InputStream initialStream = multipartFile.getInputStream(); byte[] buffer = new byte[initialStream.available()]; initialStream.read(buffer); File targetFile = new File("src/main/resources/targetFile.tmp"); try (OutputStream outStream = new FileOutputStream(targetFile)) { outStream.write(buffer); } EmailService emailService = new EmailService(); emailService.sendmail(Arrays.asList(targetFile, dailyReportFile, shiftReportFile)); model.addAttribute("msg", "Thanks for reporting.\nIt helps us improve the app."); return "messageView"; }
我尝试过上述代码,但依旧抛出以下报错信息:
java.io.FileNotFoundException:
/tmp/tomcat.8080.486619881437185002/work/Tomcat/localhost/ROOT/upload_43ba6a8d_27c9_49de_a73e_4f9026fc852e_00000000.tmp
(No such file or directory) at java.io.FileInputStream.open0(Native
Method) ~[na:1.8.0_292] at
java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_292]
at java.io.FileInputStream.(FileInputStream.java:138)
~[na:1.8.0_292] at
org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:194)
~[tomcat-embed-core-9.0.45.jar:9.0.45] at
org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
~[tomcat-embed-core-9.0.45.jar:9.0.45] at
编辑
存在同类问题,但其中提供的所有解决方案均无效。
我还尝试了以下代码,依旧运行失败
@GetMapping("/reportError") public String error(Model model) throws MessagingException, IOException { EmailService emailService = new EmailService(); File uploadedFile = convert(multipartFile); System.out.println("transferTo completed"); emailService.sendmail(Arrays.asList(uploadedFile, dailyReportFile, shiftReportFile)); model.addAttribute("msg", "Thanks for reporting.\nIt helps us improve the app."); return "messageView"; } public File convert(MultipartFile file) throws IOException { File convFile = new File(file.getOriginalFilename()); convFile.createNewFile(); FileOutputStream fos = new FileOutputStream(convFile); fos.write(file.getBytes()); fos.close(); return convFile; }
内容的提问来源于stack exchange,提问作者gdogra

