如何修改Tomcat上传最大文件大小?

Tomcat

1 前言

一个问题,一篇文章,一出故事。
笔者通过上一章节设置允许开发自行上传APP,详细请参阅以下章节,

如何配置Tomcat管理页面?


结果开发APP上传一半就出错,详细报错笔者通过如下命令可以捕抓到,

tail -f /usr/tomcat/apache-tomcat-8.5.81/logs/manager.*.log

可见如下输出,

09-Aug-2022 15:55:55.205 SEVERE [http-nio-8080-exec-7] org.apache.catalina.core.ApplicationContext.log HTMLManager: 失败 - 部署上传失败,异 常信息:[org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (96649199) exceeds the configured maximum (52428800)]
        java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (96649199) exceeds the configured maximum (52428800)
                at org.apache.catalina.connector.Request.parseParts(Request.java:3013)
                at org.apache.catalina.connector.Request.parseParameters(Request.java:3302)
                at org.apache.catalina.connector.Request.getParameter(Request.java:1168)
                at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
                at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:127)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:126)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:109)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
                at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:662)
                at org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:378)
                at org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:56)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
                at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:367)
                at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:639)
                at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
                at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:882)
                at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1691)
                at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
                at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
                at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
                at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
                at java.base/java.lang.Thread.run(Thread.java:832)
        Caused by: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (96649199) exceeds the configured maximum (52428800)
                at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:161)
                at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:205)
                at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:224)
                at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.(FileItemIteratorImpl.java:142)
                at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:252)
                at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:276)
                at org.apache.catalina.connector.Request.parseParts(Request.java:2971)
                ... 34 more

2 最佳实践

2.1 修改Tomcat配置

cp /usr/tomcat/apache-tomcat-8.5.81/webapps/manager/WEB-INF/web.xml /usr/tomcat/apache-tomcat-8.5.81/webapps/manager/WEB-INF/web.xml.default
vim /usr/tomcat/apache-tomcat-8.5.81/webapps/manager/WEB-INF/web.xml

修改如下配置,

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">
  <servlet>
    <multipart-config>
      <!-- 50MB max -->
      <max-file-size>-1</max-file-size>
      <max-request-size>-1</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>
  </servlet>
</web-app>

以上需要注意的是,
– 修改“max-file-size”值为“-1”表示不限制
– 修改“max-request-size”值为“-1”表示不限制
– 如果需要限制50MB计算方式是“50*1024*1024”即“52428800”

2.2 重启服务使配置生效

systemctl restart tomcat.service
systemctl status tomcat.service
没有评论

发表回复

Tomcat
如何配置Tomcat的日志轮转?

1 前言 一个问题,一篇文章,一出故事。 笔者之前为Tomcat日志自动分割写过一个脚本,最近发现如 …

Tomcat
如何用JSSE配置Tomcat 9 HTTPS?

1 前言 一个问题,一篇文章,一出故事。 笔者需要在Tomcat生产环境启用Tomcat的HTTPS …

Tomcat
如何用OpenSSL SSL/TLS配置Tomcat 9 HTTPS?

1 前言 一个问题,一篇文章,一出故事。 笔者需要在Tomcat生产环境启用Tomcat的HTTPS …