上传文件报错:Processing of multipart/form-data request failed. Stream ended unexpectedly

时间:2024-05-20 08:23:46

web项目中,本地开发环境上传大文件没有问题,但是部署到服务器后上传大文件却不可以,看了服务器上tomcat下产生的日志,错误信息为:

2017-08-16 12:58:55,164-[OUT-WARN] http-9080-10 org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest[64] - Unable to parse request
org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parseRequest(JakartaMultiPartRequest.java:189)
at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.processUpload(JakartaMultiPartRequest.java:127)
at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:92)
at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:81)
at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:803)
at org.apache.struts2.dispatcher.ng.PrepareOperations.wrapRequest(PrepareOperations.java:134)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.founder.fwpt.servlet.ProcessFilter.doFilter(ProcessFilter.java:32)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.ha.tcp.ReplicationValve.invoke(ReplicationValve.java:347)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:1005)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:903)
at java.io.InputStream.read(InputStream.java:82)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:347)
... 23 more

一开始认为是网络的原因,网络超时而没有上传成功,然后百度寻找解决办法,如下:

1、首先,上传页面上面的 form 标签要设enctype="multipart/form-data" 参数

2、修改tomcat配置文件server.xml:

<Connector port="8086" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
将上面的参数disableUploadTimeout值改为false即可。

3、修改了tomcat配置文件server.xml中的另外一处:

<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" 
redirectPort="8443" /> 在里边加入disableUploadTimeout="false"一句。

以上解决方法在我的程序中已经设置,所以仍然不能解决问题。

然后无意中查看页面和js代码,发现一个问题:添加信息页面,在点击保存按钮后进行表单验证再form提交,然后提示添加成功信息,关闭添加tab页面,刷新list列表数据,但这就存在一个问题,如果上传文件过大就会导致文件没有上传完成而直接跳转到了list列表页面,进而导致上传失败。

解决方法:form提交后,不提示信息刷新数据,在页面重新加载的时候在进行提示、刷新。

上传文件报错:Processing of multipart/form-data request failed. Stream ended unexpectedly

上传文件报错:Processing of multipart/form-data request failed. Stream ended unexpectedly

问题解决。