springMVC上传大文件浏览器重定向

来源:互联网 发布:如何入侵网站数据库 编辑:程序博客网 时间:2024/05/17 22:45

现象:

假如限制用户上传文件的大小为200KB,用户上传300KB的数据时,可以被springMVC拦截并提示用户,但是如果用户上传远超过限制的文件(如:30MB)时,浏览器重定向,如图所示:


原因:

这种现象只在tomcat中出现,在jetty中可以正常运行。
引用:Apache Tomcat 8 Configuration Reference
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将要优先拦截请求并且重定向。

解决方法1:

将Tomcat/conf/server.xml中的maxSwallowSize设置为负数。(注意:如果是用eclipse部署tomcat,修改workspace中的severs下的server.xml)
<Connector port="8080" protocol="HTTP/1.1"           connectionTimeout="20000"           redirectPort="8443"           maxSwallowSize="-1" />
解决方法1适合于开发环境下,不适合于实际运行环境。因为这种方法会浪费带宽。

解决方法2

(1)将tomcat的server.xml文件总maxSwallowSize设置为一个合适的大小,如11mb(11 x 1024 x 1024 = bytes)
<Connector port="8080" protocol="HTTP/1.1"           connectionTimeout="20000"           redirectPort="8443"           maxSwallowSize="11534336" />


(2)在client端上传之前,通过JavaScript进行大小验证。



2 0
原创粉丝点击