取消Tomcat对POST请求长度限制

1 Request header is too large

从字面意思可知,是请求头过大,设置connector的maxHttpHeaderSize为大点的值即可,单位byte,默认值:8192 (8 KB)。官方文档原文如下:http://tomcat.apache.org/tomcat-9.0-doc/config/http.html#Standard_Implementation
maxHttpHeaderSize :
The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 8192 (8 KB).

注意,官方文档并没有说明如何将该值设置为无限制,所以尽量不要将其设置为负数以免程序出错。
示例如下在tomcat文件夹下的conf文件中的server.xml


<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" 
    maxHttpHeaderSize="16384"/>

2 Request Entity Too Large

从字面意思可知,是请求体过大,多见于文件上传时触发,设置connector的maxPostSize为大点的值即可,单位byte,默认值:2097152 (2 megabytes)。若想将其设为无限制,则将其设置为负数即可。网上解决方案大多说设置为0,这是错的,设置为0会导致tomcat截断post的参数,导致后台接收到的参数全都是null。官方文档原文如下:http://tomcat.apache.org/tomcat-9.0-doc/config/http.html#Common_Attributes
maxPostSize : The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes).

注意,官方文档说的是less than zero,也就是说设置为小于0的值才是设置maxPostSize为无限制,网上的大多数解决方法说的设置为0是错的。


<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" 
    maxPostSize="209715200"/>

3 综合方案

综上,避免 Request header is too largeRequest Entity Too Large的最好的配置就是同时配置俩属性为较大的值,避免不必要的麻烦。示例如下:在tomcat文件夹下的conf文件中的server.xml


<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" 
    maxPostSize="209715200" maxHttpHeaderSize="16384"/>

4 懒人方案(不建议)

任何东西太大都不好,会造成不必要的麻烦,如果无法估计配置的大小,可以设置为不限制大小

  • 在tomcat文件夹下的conf文件中的server.xml 配置中添加:
  • maxPostSize="-1" //-1 表示不限制大小
  • maxPostSize:指定POST方式请求的最大量,没有指定默认为2097152。
  • maxHttpHeaderSize =“102400”
  • maxHttpHeaderSize:HTTP请求和响应头的最大量,以字节为单位,默认值为4096字节
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" 
    maxPostSize="-1" maxHttpHeaderSize="102400"/>

你可能感兴趣的:(java,too,large,Request,Entity,Request,header,tomcat大小限制,post请求长度)