Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring upload file size limit error

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.

like image 502
Ryan Fasching Avatar asked Jul 18 '18 20:07

Ryan Fasching


People also ask

What is default multipart file upload size in spring boot?

The default is 10MB. file-size-threshold specifies the size threshold after which files will be written to disk.

How do you handle MaxUploadSizeExceededException?

And to handle the MaxUploadSizeExceededException you can add ControllerAdvice with ExceptionHandler. otherwise that exception will be triggered before the request is mapped to controller.

What is spring boot MultipartFile?

public interface MultipartFile extends InputStreamSource. A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired ...


2 Answers

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
like image 135
vikash singh Avatar answered Sep 23 '22 10:09

vikash singh


As per latest Spring Boot Common properties below should work

MULTIPART (MultipartProperties)

spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB # Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

or If you just want to control the multipart properties then multipart.max-file-size and multipart.max-request-size properties should work.

multipart.max-file-size=5MB
multipart.max-request-size=5MB
like image 28
kj007 Avatar answered Sep 23 '22 10:09

kj007