POST请求数据,出现Post too large...错误

1.问题场景
java web开发当中,使用apache tomcat作为服务器,使用POST请求数据时,可能会出现:Post too large...的情况,错误信息:Parameters were not parsed because the size of the posted data was too big. Use the maxPostSize attribute of the connector to resolve this if the application should accept large POSTs.


2.问题原因

Q: In Tomcat, I got a "Post data too big" error.

A: Apache Tomcat by default sets a limit on the maximum size of HTTP POST requests it accepts.
   In Tomcat 5, this limit is set to 2097152 (2 Mb). When you try to upload files or post forms that are
   larger than 2 MB, this error can occur.

   The solution is to reconfigure Tomcat to accept larger POST requests, either by increasing
   the limit, or by disabling it. This can be done by editing Tomcat's server.xml. 
   In the element, add an attribute "maxPostSize" and set a larger value (in bytes) to
   increase the limit. Setting it to 0 will disable the size check.

即在conf/server.xml中的标签处, maxPostSize属性值被设置成 2097152 (2M),导致在post请求数据时,参数最大长度限制是   2097152。

3.解决办法
maxPostSize参数值设置成0,或者删除这个属性即可。

4.问题延伸
  (1).有些时候需要设置该参数值以达到使用tomcat拦截大数据请求的场景,但是设置 maxPostSize属性值不成功的场景。
网上解释是: As I understand it, Tomcat only enforces that limit if the content type is  application/x-www-form-urlencoded . For multiparts you'll have to read the stream yourself     and enforce the limit yourself. A great tool for working with multipart data is  Apache FileUpload .

即tomcat能够处理拦截的请求的contextType需要是application/x-www-form-urlencoded

   (2).对于java中String字符串,
        a.如果采用new Stirng()的形式创建String对象,那么限制的长度是 Integer.MAX_VALUE = 2147483647 (差不多是4G)
          解析原因:String对象在内存中是以字符数据的形式存在,那么创建一个字符数据:
                        char[] str = new char[ Integer.MAX_VALUE ];  // 可以看出字符数组的最大长度是 Integer.MAX_VALUE
        b.采用直接量创建String时,ASCII字符长度为65534个
           解析原因:CONSTANT_Utf8_info表中使用一个16位的无符号整数来记录字符串的长度的,最多能表示 65536个字节,而java的.class文件是使用一种变体UTF-8格式来存放字符的,null值使用两个字节来表示,因此只剩下 65536 - 2 =   65534个字节。如果字符串中含有中文等非ASCII字符,那么双引号中字符的数量会更少( 一个中文字符占用三个字节)。
                  String str = "abd...";
 



你可能感兴趣的:(J2EE)