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:
- Multipart File upload Spring Boot
- Setting max file size for upload when running on Tomcat
- MaxUploadSizeExceededException in Spring - but the suggested MultipartResolver bean does not compile for me because class CommonsMultipartResolver does not exist
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.