org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed

文件上传报org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed

先上解决方式:

在yml或者其他配置文件中添加如下配置即可

Spring:
mvc:
  hiddenmethod:
    filter:
      enabled: true

问题解决完毕、梳理下问题点【可忽略如下内容】

最近突然发现项目的上传功能失效了,一顿操作发现错误是:

org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed

尝试着各种解决未果,百度又是各种牛头不对马嘴。折腾了几个小时,皇天不负有心人。终于找到了一位老哥在gitmemory上提交的问题

url:https://www.gitmemory.com/issue/spring-projects/spring-boot/18644/583836701

问题:

Upload file org.springframework.web.multipart.MultipartException

When updating Spring Boot to version 2.2.0 I get an error when trying to upload a file.

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

Error code

@RestController
public class FileController {
	@PostMapping("/api/file")
	public ResponseEntity<String> upload(@RequestParam("file") MultipartFile multipartFile)  {
		//...		
	}
}
This code worked for me on versions less than 2.2.0

思路:

I ran into the same issue.

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

Worked fine on Spring Boot 2.1.12, failed after upgrade to 2.2.4 (both versions run the same embedded Tomcat 9.0.30).

The root cause was that we were using logback-access filter that logs requests and responses. The filter wrapped HttpServletRequest with a caching wrapper and closed the input stream before passing the request along the filter chain. This caused an issue when javax.servlet.http.HttpServletRequest#getParts was called, as that call is delegated to the original Tomcat's request (with a now-closed input stream), which then results in the "stream closed" exception.

The Spring change that triggered this issue is hiddenHttpMethodFilter no longer being present in the filter chain. Previously this filter would be one of the first filters to be called, and it would call HttpServletRequest#getParameter, which in turn would make Tomcat's request implementation parse multipart content and store the parts in the object (so that later calls to #getParts would use that and not try to parse them again).

I reported this to Logback: https://jira.qos.ch/browse/LOGBACK-1503

Workaround - call HttpServletRequest#getParameter before passing the request to logback's filter (see LOGBACK-1503 for code), to force Tomcat to parse and store request parts while input stream is still open.

logback链接:

https://jira.qos.ch/browse/LOGBACK-1503

解析:

上面的老哥分析我看的也是一致半解

他是项目在升级到springboot2.2+时候出现的问题与我的问题一致,简单的查看可知在springboot2.2+中把 META-INF/spring-configuration-metadata.json 配置文件中把spring.mvc.hiddenmethod.filter.enabled设置为了false

{
      "name": "spring.mvc.hiddenmethod.filter.enabled",
      "type": "java.lang.Boolean",
      "description": "Whether to enable Spring's HiddenHttpMethodFilter.",
      "defaultValue": false
    }

至于为什么会出现这个问题,emmm我还得梳理下源码【待补充】

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
### 回答1: 解析多部分servlet请求失败;嵌套异常是java.io.ioexceptionorg.apache.tomcat.util.http.fileupload.fileuploadexception:流关闭。 ### 回答2: 这个错误的意思是解析多部分Servlet请求失败,其中嵌套的异常是Java.IO.IO异常:org.apache.tomcat.util.http.fileupload.FileUploadException: Stream Closed。出现这个错误的原因可能是由于文件上传中出现了某种问题。 Java Web应用程序中的Servlet通常用于处理表单提交中的文件上传。使用Servlet的文件上传功能需要使用Apache Tomcat的Multipart解析器。当请求中含有文件上传时,即使解析器收到了请求,仍然可能出现解析错误。 在处理文件上传请求时,Servlet通常需要修改Multipart解析器的一些默认参数值。如果这些参数值不正确,就会出现解析错误,造成打开文件流失败的情况。 这个错误还可能源于文件上传过程中的网络波动或服务器配置不当等因素。例如服务器配置不当,文件大小可能会超过服务器所能处理的最大限制,导致文件上传失败。 如果实在无法解决这个问题,建议换一个网页App来做文件上传,例如:Dropbox、WeTransfer等,它们都是专门为文件上传设计的,可以在传输过程中保持文件的完整性。 ### 回答3: 这个错误通常是由于在处理多部分HTTP请求时出现问题而导致的。当我们在使用HTTP进行文件上传或表单提交等操作时,服务器需要解析从客户端发送过来的请求报文。如果解析过程中发生了错误,就会出现“Failed to parse multipart servlet request”的错误提示。 其中,“nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed”是一个嵌套异常,意思是出现了一个名为FileUploadException的异常,而这个异常的“Stream closed”提示是它的一个子异常,表示在解析请求报文时,上传文件的流读取被关闭了。 这个错误通常由以下几个原因引起: 1. 客户端发送的请求报文格式不符合服务器的要求,比如上传的文件大小超出服务器的限制; 2. 服务器端的代码没有正确的处理多部分请求,例如没有正确的将上传的文件保存到指定的位置; 3. 服务器端使用的是老版本的Tomcat服务器,在解析多部分请求时出现问题。 针对这个错误,可以通过以下几种方法进行解决: 1. 从客户端出发,检查是否符合服务器要求的上传文件大小等要求; 2. 检查服务器端的代码是否正确处理了多部分请求,例如是否正确将上传的文件保存到了指定的位置; 3. 如果使用的是老版本的Tomcat服务器,可以尝试升级到最新版本,或者使用其他容器(如Jetty等)进行上传操作。 总之,我们在处理多部分请求时需要仔细检查报文格式,以及对服务器端代码的逻辑进行仔细的审查,从而减少出现这种错误的可能性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值