JSP使用FileUpload上傳檔案設定setSizeMax後連線被重置

indexman發表於2017-12-28

今天在學習利用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" />



重啟後測試正常!













相關文章