-1

I am updating a Java app from a very old version of Spring Boot to version 3.2.1. One of the functions of this app is uploading files to S3. I am able to upload files smaller than 1MB with no problem, but uploading larger files gets a 422 error with: message : "Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes."

I have set these application.yml parameters and verified them by printing them out at runtime:

spring:
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB
      max-request-size: 10MB
server:
  tomcat:
    max-swallow-size: -1
    max-http-form-post-size: -1

I have tried adding this bean to my application, and verified that it is running during app startup:

@Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(DataSize.ofBytes(512000000L));
        factory.setMaxRequestSize(DataSize.ofBytes(512000000L));
        return factory.createMultipartConfig();
    }

I think these attempts to change the max file size are not working because the maximum listed in the error message does not change.

I have consulted numerous StackOverflow and other articles, including these:

I am using version 3.2.1 of of these libraries (plus many more):

  • spring-boot-starter
  • spring-boot-starter-actuator
  • spring-boot-starter-hateoas
  • spring-boot-starter-data-jpa
  • spring-boot-starter-data-rest

... and embedded Tomcat 10.1.17 is automatically included.

I tracked through the Tomcat code, and set breakpoints at in Request.parseParts() and FileUploadBase.setSizeMax(). Interestingly, when I upload a file smaller than 1MB, the breakpoints are hit and the max file size is the value I configured. When I upload a larger file, these breakpoints are NOT hit. So some other code somewhere must be enforcing the max size using a different limit.

At this point I am stuck because I have tried every suggestion that I can find.

1
  • Thank you for sharing @Bean MultipartConfigElement which, in my case, was required to get Spring Boot file upload stop complaining with "java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided"
    – yglodt
    Commented May 29 at 11:02

1 Answer 1

0

Turns out that the path from the browser to the back-end service goes through another service, which is configured to allow 25MB uploads but not when running in development mode - which is the way I am testing. So that service is limiting the file upload size to 1MB, and producing the error. That explains why:

  • I couldn't breakpoint on the error in my back-end service
  • Logging and other techniques for finding the problem in my back-end service were of no use for this issue
  • I had no problem when uploading larger files in production

Sheesh!

Not the answer you're looking for? Browse other questions tagged or ask your own question.