[tomcat]post 数据, 服务端居然收不到

起因

某个功能通过post提交数据,但服务端却接收不到数据。

过程

查看 tomcat 日志,确定参数没有被解析是因为超过maxPostSize的值。

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.

相关代码

org.apache.catalina.connector.Request#parseParameters

      protected void parseParameters() {
       …
            int len = getContentLength();
            if (len > 0) {
                int maxPostSize = connector.getMaxPostSize();
                if ((maxPostSize >= 0) && (len > maxPostSize)) {
                    Context context = getContext();
                    if (context != null && context.getLogger().isDebugEnabled()) {
                        context.getLogger().debug(
                                sm.getString("coyoteRequest.postTooLarge"));
                    }
                    checkSwallowInput();
                    parameters.setParseFailedReason(FailReason.POST_TOO_LARGE);
                    return;
                }



  // 在connector中也可以看到默认值和doc
   /**
     * Maximum size of a POST which will be automatically parsed by the
     * container. 2MB by default.
     */
    protected int maxPostSize = 2 * 1024 * 1024;
……

从connector取出maxPostSize和content-length比较, 如果maxPostSize大于0并且小于len,忽略解析参数并打印debug日志。

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.

这里注意 apache-tomcat-7.0.63之前的版本略有不同, 设置为 0 表示不限制 POST 大小。

总结

Http 标准并没有限制大小,但是没个实现都有自己的限制,如:

· Tomcat 2mb,可以通过maxPostSize修改

· Nginx 1mb, 可通过client_max_body_size修改

· Apache 2mb,可通过 LimitRequestBody修改

这些是默认值,可按需调整。

參考

3.3. Message Body 描述了Content-Length 配置相关的内容。

你可能感兴趣的:([tomcat]post 数据, 服务端居然收不到)