上周将项目Spring Boot Parent依赖从2.1.3升级到最新的2.2.6,原来工作好好的文件上传出问题,总是报multipart文件解析错误,文件流被关闭,百度上找了好多地方法都没查到,后到国外谷哥网站收索到解决方法。
Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
先在百度网站上检索这个错误的信息,找了无数页,都没什么用,很多都是提临时目录找不到的问题,让重启和增加临时文件配置,但试了几次,没任何作用,应该不是这个问题。
更多的是让检查程序,但程序以前没有任何问题,不可能是代码关闭流引起的问题。
坑了一天,还是解决不了。没办法,还是翻一下,上谷哥检索,在github的问题上找到了线索,是Spring Boot 升级到2.2.2之后的问题。
根据英文是由logback-access造成的,spring改变了hiddenHttpMethodFilter的过滤机制,需要找Tomcat的请求执行机制。
the root cause was that were using logback-access filter that logs requests and responses.
The Spring change that triggered this issue is hiddenHttpMethodFilter on longer being present in the filter chain. which in turn would make Tomcat’s request implementation parse multipart content and store the parts in the object.
解决办法,配置文件增加下面内容:
#解决升版后文件上传问题
spring:
mvc:
hiddenmethod:
filter:
enabled: true