java web开发,tomcat作为服务器,post请求的长度限制,出现Post too larg

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 <Connector> 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中的<Connector>标签处, maxPostSize属性值被设置成 2097152 (2M),导致在post请求数据时,参数最大长度限制是 2097152。


3.解决办法

maxPostSize参数值设置成0,或者删除这个属性即可。

http://stackoverflow.com/questions/12476604/processing-of-multipart-form-data-request-failed-read-timed-out


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...";


转载于:https://my.oschina.net/zyong/blog/336186

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值