JSP使用FileUpload上传文件设置setSizeMax后连接被重置
今天在学习利用JSP+Servlet+FileUpload开发文件上传功能。在设置了上传文件最大限制为10M以后,选择了3M的pdf上传提交后连接被重置了。。。
我的代码:
//设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
upload.setFileSizeMax(1024 * 1024);
//设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为10MB
upload.setSizeMax(1024 * 1024 * 10);
一头雾水,
后来在stackoverflow上找到了答案,这个和Tomcat默认设置有关,server.xml中有个重要参数
maxSwallowSize 默认为2M,改为-1表示无限制
maxSwallowSize
The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored but the client still sends it. If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2097152 (2 megabytes) will be used. A value of less than zero indicates that no limit should be enforced.
解决方案:
修改tomcat配置文件server.xml
修改前:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改后:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" connectionUploadTimeout="36000000" disableUploadTimeout="false" maxSwallowSize="-1"
redirectPort="8443" />
重启后测试正常!