[import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete

控制台出现报错:

20:29:18.642 [http-nio-8888-exec-16] WARN  o.s.w.m.s.StandardServletMultipartResolver - [cleanupMultipart,139] 
- Failed to perform cleanup of multipart items
java.io.UncheckedIOException: Cannot delete C:\Users\Administrator\AppData\Local\Temp\tomcat.8888.15364175900338092022
\work\Tomcat\localhost\ROOT\upload_663794fd_09ab_4322_81ed_66b73830d698_00000000.tmp
	at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.delete(DiskFileItem.java:431)
	at org.apache.catalina.core.ApplicationPart.delete(ApplicationPart.java:53)
	at org.springframework.web.multipart.support.StandardServletMultipartResolver
.cleanupMultipart(StandardServletMultipartResolver.java:134)

报错截图:

[import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete_第1张图片

 报错触发场景:在上传Excel文件时,出现此报错,观察上传的内容能够正常存储到数据库,但就是会出现以上报错信息.

报错原因分析:从文件上传接口,上传Excel文件,然后解析上传的文件,并进行数据存储.此过程需要在接口参MultipartFile获取输入流InputStream,才能真正得到后台可以处理的数据.但是如果使用完InputStream,而没有主动的去关闭此输入流,就会出现以上报错信息.

实质的提示信息是,流连接没有关闭,导致Tomcat临时文件无法删除.

[import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete_第2张图片

 解决办法:

1.添加finally代码块中主动释放资源:
可以使用工具方法:导入工具方法import org.apache.poi.util.IOUtils;或者inputStream.close()方法,但此close()方法依然需要进行try-catch处理.

[import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete_第3张图片 [import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete_第4张图片

2.可以使用 Lombok 框架 @Cleanup 注解,主动释放资源:

[import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete_第5张图片

 3.使用try(){}catch{},主动释放资源的方法:即将流连接资源放到try后的括号内,达到主动释放资源的效果:

[import]报错:Failed to perform cleanup of multipart itemsjava.io.UncheckedIOException: Cannot delete_第6张图片

 以上三种方法均亲测有效,均能有效释放资源,解决报错问题.

你可能感兴趣的:(Java,Tomcat,java,tomcat)