Java HttpRequest在tomcat中允许的最大大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2947683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:49:33  来源:igfitidea点击:

HttpRequest maximum allowable size in tomcat?

javatomcat

提问by mabuzer

What is the maximum data size I can send in a single HttpURLConnectionto Tomcat? Is there any limitation for the request size?

我可以单次发送的最大数据大小HttpURLConnectionTomcat多少?请求大小有限制吗?

采纳答案by stacker

The connector sectionhas the parameter

连接器部具有参数

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 or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

将由容器 FORM URL 参数解析处理的 POST 的最大大小(以字节为单位)。可以通过将此属性设置为小于或等于 0 的值来禁用该限制。如果未指定,则此属性设置为 2097152(2 兆字节)。

Another Limit is:

另一个限制是:

maxHttpHeaderSizeThe maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 4096 (4 KB).

maxHttpHeaderSize请求和响应 HTTP 标头的最大大小,以字节为单位。如果未指定,则此属性设置为 4096 (4 KB)。

You find them in

你在

$TOMCAT_HOME/conf/server.xml

回答by Freeman

You have to modify two possible limits:

您必须修改两个可能的限制:

In conf\server.xml

conf\server.xml

<Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
                maxPostSize="67589953" />

In webapps\manager\WEB-INF\web.xml

webapps\manager\WEB-INF\web.xml

<multipart-config>
  <!-- 52MB max -->
  <max-file-size>52428800</max-file-size>
  <max-request-size>52428800</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>

回答by Alejandro Hdez. Angeles

Just to add to the answers, App Server Apache Geronimo 3.0 uses Tomcat 7 as the web server, and in that environment the file server.xml is located at <%GERONIMO_HOME%>/var/catalina/server.xml.

只是为了补充答案,App Server Apache Geronimo 3.0 使用 Tomcat 7 作为 Web 服务器,在该环境中,文件 server.xml 位于 <%GERONIMO_HOME%>/var/catalina/server.xml.

The configuration does take effect even when the Geronimo Console at Application Server->WebServer->TomcatWebConnector->maxPostSizestill displays 2097152 (the default value)

即使 Geronimo 控制台Application Server->WebServer->TomcatWebConnector->maxPostSize仍然显示 2097152(默认值),配置也会生效

回答by vallismortis

Although other answers include some of the following information, this is the absolute minimum that needs to be changed on EC2 instances, specifically regarding deployment of large WAR files, and is the least likely to cause issues during future updates. I've been running into these limits about every other year due to the ever-increasing size of the Jenkins WAR file (now ~72MB).

尽管其他答案包括以下一些信息,但这是 EC2 实例上需要更改的绝对最小值,特别是关于大型 WAR 文件的部署,并且在未来更新期间最不可能导致问题。由于 Jenkins WAR 文件的大小不断增加(现在约为 72MB),我每隔一年就会遇到这些限制。

More specifically, this answer is applicable if you encounter a variant of the following error in catalina.out:

更具体地说,如果您在 中遇到以下错误的变体,则此答案适用catalina.out

SEVERE [https-jsse-nio-8443-exec-17] org.apache.catalina.core.ApplicationContext.log HTMLManager:
FAIL - Deploy Upload Failed, Exception:
[org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException:
the request was rejected because its size (75333656) exceeds the configured maximum (52428800)]

On Amazon EC2 Linux instances, the onlyfile that needs to be modified from the default installation of Tomcat (sudo yum install tomcat8) is:

在 Amazon EC2 Linux 实例上,唯一需要从 Tomcat ( sudo yum install tomcat8)的默认安装修改的文件是:

/usr/share/tomcat8/webapps/manager/WEB-INF/web.xml

By default, the maximum upload size is exactly 50MB:

默认情况下,最大上传大小正好是 50MB:

<multipart-config>
  <!-- 50MB max -->
  <max-file-size>52428800</max-file-size>
  <max-request-size>52428800</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>

There are only two values that need to be modified (max-file-sizeand max-request-size):

只有两个值需要修改(max-file-sizemax-request-size):

<multipart-config>
  <!-- 100MB max -->
  <max-file-size>104857600</max-file-size>
  <max-request-size>104857600</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>

When Tomcat is upgraded on these instances, the new version of the manager web.xmlwill be placed in web.xml.rpmnew, so any modifications to the original file will not be overwritten during future updates.

在这些实例上升级 Tomcat 时,新版本的管理器web.xml将放置在 中web.xml.rpmnew,因此在以后的更新中不会覆盖对原始文件的任何修改。

回答by Fangming

The full answer

完整的答案

1. The default (fresh install of tomcat)

1.默认(tomcat全新安装)

When you download tomcat from their official website (of today that's tomcat version 9.0.26), all the apps you installed to tomcat can handle HTTP requests of unlimited size, given that the apps themselves do not have any limits on request size.

当您从他们的官方网站(今天的 tomcat 版本 9.0.26)下载 tomcat 时,您安装到 tomcat 的所有应用程序都可以处理无限大小的HTTP 请求,因为应用程序本身对请求大小没有任何限制。

However, when you try to upload an app in tomcat's manager app, that app has a default war file limit of 50MB. If you're trying to install Jenkins for example which is 77 MB as ot today, it will fail.

但是,当您尝试在 tomcat 的管理器应用程序中上传应用程序时,该应用程序的默认 war 文件限制为50MB。例如,如果您尝试安装 Jenkins,例如今天的 77 MB,它将失败。

2. Configure tomcat's per port http request size limit

2.配置tomcat的每端口http请求大小限制

Tomcat itself has size limit for each port, and this is defined in conf\server.xml. This is controlled by maxPostSizeattribute of each Connector(port). If this attribute does not exist, which it is by default, there is no limiton the request size.

Tomcat 本身对每个端口都有大小限制,这在conf\server.xml. 这是由maxPostSize每个Connector(端口)的属性控制的。如果此属性不存在(默认情况下),则对请求大小没有限制

To add a limit to a specific port, set a byte size for the attribute. For example, the below config for the default 8080 port limits request size to 200 MB. This means that all the apps installed under port 8080 now has the size limit of 200MB

要对特定端口添加限制,请为属性设置字节大小。例如,默认 8080 端口的以下配置将请求大小限制为 200 MB。这意味着所有安装在 8080 端口下的应用程序现在都有 200MB 的大小限制

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

3. Configure app level size limit

3. 配置应用级大小限制

After passing the port level size limit, you can still configure app level limit. This also means that app level limit should be less than port level limit. The limit can be done through annotation within each servlet, or in the web.xml file. Again, if this is not set at all, there is no limiton request size.

通过端口级别大小限制后,您仍然可以配置应用程序级别限制。这也意味着应用程序级别限制应小于端口级别限制。可以通过每个 servlet 或 web.xml 文件中的注释来完成限制。同样,如果根本没有设置,则请求大小没有限制

To set limit through java annotation

通过java注解设置限制

@WebServlet("/uploadFiles")
@MultipartConfig( fileSizeThreshold = 0, maxFileSize = 209715200, maxRequestSize = 209715200)
public class FileUploadServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        // ...
    }
}

To set limit through web.xml

通过 web.xml 设置限制

<web-app>
  ...
  <servlet>
    ...
    <multipart-config>
      <file-size-threshold>0</file-size-threshold>
      <max-file-size>209715200</max-file-size>
      <max-request-size>209715200</max-request-size>
    </multipart-config>
    ...
  </servlet>
  ...
</web-app>

4. Appendix - If you see file upload size error when trying to install app through Tomcat's Manager app

4. 附录 - 如果您在尝试通过 Tomcat 的 Manager 应用程序安装应用程序时看到文件上传大小错误

Tomcat's Manager app (by default localhost:8080/manager) is nothing but a default web app. By default that app has a web.xmlconfiguration of request limit of 50MB. To install (upload) app with size greater than 50MB through this manager app, you have to change the limit. Open the manager app's web.xml file from webapps\manager\WEB-INF\web.xmland follow the above guide to change the size limit and finally restart tomcat.

Tomcat 的 Manager 应用程序(默认为 localhost:8080/manager)只不过是一个默认的 Web 应用程序。默认情况下,该应用程序web.xml的请求限制配置为50MB。要通过此管理器应用程序安装(上传)大小大于 50MB 的应用程序,您必须更改限制。从中打开管理器应用程序的 web.xml 文件webapps\manager\WEB-INF\web.xml并按照上述指南更改大小限制,最后重新启动 tomcat。