How to increase the allowed maximum multipart file size on Spring 6 (Tomcat 10) using multipartResolver = StandardServletMultipartResolver

139 views Asked by At

Have trawled and tried so many posts around this topic...reaching out for help

It is a Spring 6 application running in Tomcat 10 With a servlet that accepts multi part files we get this error. Below a list of key config items and code:

Error:

08:51:46.440 [https-jsse-nio-9443-exec-29] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver -- Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded

The limit kicks in around 1.5MB file (not 128KB)

/src/resources/application.properties contains:

spring.servlet.multipart.max-file-size=10485760
spring.servlet.multipart.max-request-size=10485760
spring.servlet.multipart.file-size-threshold=10485760
server.tomcat.max-swallow-size=-1
server.tomcatmax-http-form-post-size=-1

app-context.xml contains:

<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"></bean>

tomcat context.xml contains:

<Context path="gearlog" useHttpOnly="false" allowCasualMultipartParsing="true">

All servlets containing a method that accepts Multipart file contain:

@Controller
@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024*100, 
maxFileSize=1024*1024*100, maxRequestSize=1024*1024*5*100)
public class...

Nothing we do seems to make any difference. We can uplaod files of up to around 1.5MB just fine. Any larger than that we get this error

What is this question NOT about (to disambiguate the many posts on this topic)

Not about Adding a limit. Our issue is we cannot INCREASE the limit

Not about handling limit breaches gracefully. First we need to get the limit up to a sensible one!

Not about Using CommonsMultipartResolver - that disappeared when we upgraded to Spring 6 and these issues began

Any help much appreciated as I've been banging my head on this for days :(

1

There are 1 answers

0
Kit Kline On

Answering my own question in case it helps others in the future

(a) The app had some spring-boot dependencies but was NOT a spring-boot app so application.properties was a dead end (b) Turns our that adding the following to the servlet dispatcher in Web.xml works as it disables the limit as it forwards the request to the controller (c) Placing the limits in @MultipartConfig on the controller never worked because the limit is imposed during dispatch - i.e before reaching the controller - at least that's why I think it doesn't work

Final solution (for me) was update web.xml as follows

<servlet>
    <servlet-name>api-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/api-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
      <location>/var/tmp</location>
      <max-file-size>104857600</max-file-size>
      <max-request-size>104857600</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>api-dispatcher</servlet-name>
    <url-pattern>/api/*</url-pattern>
    
</servlet-mapping>