Spring upload file size limit error

Ryan Fasching picture Ryan Fasching · Jul 18, 2018 · Viewed 20.4k times · Source

I am using Spring Boot and I can send images that are smaller than 1MB, but when I make a post request with a large image larger than 1MB I get this error:

Maximum upload size exceeded; 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 been looking around so many places to try to find the answer to this error. I have looked at all of these questions and tried implementing what they suggest to no avail: Spring upload file size limit, I am trying to set maxFileSize but it is not honored, org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException, and Max Limit of MultipartFile in spring boot

I am using version 2.0.3 of Spring and here is my post mapping:

    @PostMapping("/post")
    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
      String message = "";
      try {
        storageService.store(file);
        files.add(file.getOriginalFilename());

        message = "You successfully uploaded " + file.getOriginalFilename() + "!";
        return ResponseEntity.status(HttpStatus.OK).body(message);
      } catch (Exception e) {
      message = "FAIL to upload " + file.getOriginalFilename() + "!";
      return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(message);
  }

}

And here are all the application.properties configuration that I have tried:

1

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

2

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB

3

spring.http.multipart.max-file-size=5MB
spring.http.multipart.max-request-size=5MB

4

multipart.max-file-size=5MB
multipart.max-request-size=5MB

5

server.tomcat.max-file-size=5000000

6

server.tomcat.max-http-post-size=5000000

7

spring.servlet.multipart.maxFileSize=-1
spring.servlet.multipart.maxRequestSize=-1

Even tried changing it to application.yml:

spring:
  servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 5MB

I also looked into changing the request size allowed by Tomcat in web.xml file, but I don't have a web.xml file. The Tomcat I am using is bundled in to the app.

Answer

vikash singh picture vikash singh · Oct 8, 2018

Add the below lines in application.properties for Spring Boot version 2.0.0.RELEASE and above -

spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.servlet.multipart.enabled=true

Please note for lower versions, i.e. 1.5.9.RELEASE and below the configuration used to be as follows:

spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB