在接收到前端的文件上传请求时,在对应的controller方法中使用线程池异步处理文件相关的逻辑时,会偶尔发生java.io.FileNotFoundException异常
。
java.io.FileNotFoundException: C:\Users\zzhua195\AppData\Local\Temp\tomcat.8080.567748920478140755\work\Tomcat\localhost\ROOT\upload_8e27d0a7_9cf4_4f8a_aecc_ea051653749e_00000006.tmp (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:198)
at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getInputStream(StandardMultipartHttpServletRequest.java:254)
at com.trent.upload.service.UploadService.convertToPdf(UploadService.java:57)
at com.trent.upload.service.UploadService.lambda$dealFile$0(UploadService.java:43)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
原因是,tomcat线程在请求完成处理后,会对临时文件进行清理,文件被清理后,自然就找不到对应的文件了,这个清理应该也是异步的,当对应的controller中的异步耗时长时,几乎必现。