19

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.

Ryan Fasching
  • 449
  • 2
  • 11
  • 21
  • 1
    Did you use any gateway? or any other service to forward the request to your actually request. If yes, please add the `spring.servlet.multipart` configuration all the way from the entry point to the real service. – Troy Young Aug 22 '18 at 03:35
  • @TroyYoung - That was it for me. Thanks! – idungotnosn Aug 06 '19 at 18:57

7 Answers7

39

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
Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
vikash singh
  • 1,479
  • 1
  • 19
  • 29
  • Could you please explain, why you mention `spring.http` and `spring.servlet`? Did you set both? – Peter Wippermann Mar 12 '19 at 11:22
  • As it is mentioned in the answer for spring version 2.0.1 we need to use spring.servlet and for spring version 2.x we need to use spring.http. – vikash singh Mar 13 '19 at 02:31
  • Oh, I didn't notice the remark regarding the version numbers. Thanks for clarifying! I edited your post in that regard. Hope that's ok for you. – Peter Wippermann Mar 13 '19 at 15:43
  • Can anyone help me understand why we would need to set. 'spring.servlet.multipart.enabled=true' if by default it is true. I ran into a recent project where multipart was not working and we had to set it to enabled = true we are still really puzzled why.. https://github.com/spring-projects/spring-boot/blob/8bc780762a44c7d57e6ddf15abb7071f499857ce/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java – Naomi See May 28 '20 at 17:32
  • spring.servlet.multipart.enabled=true just make my day! – Erik Sep 11 '20 at 09:48
1

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
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
kj007
  • 6,073
  • 4
  • 29
  • 47
1

Dependes on Spring boot Version , example 1.X, on your application.properties, set the file size limit:

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

Spring Boot 2.x / above:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
Tiago Medici
  • 1,944
  • 22
  • 22
0

This is what I have and it works perfectly for me for the backend.

@PutMapping(value = USER_PROFILE_UPDATE_URL_MAPPING)
  public String updateProfile(
      @ModelAttribute(AUTHORIZED_USER_MODEL_KEY) User user,
      BindingResult bindingResult,
      @RequestParam(name = "file") MultipartFile multipartFile,
      Model model, @RequestParam Map<String, String> params) {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    User authorizedUser = (User) authentication.getPrincipal();
    //.... 

      return REDIRECT_TO_LOGIN;
    }

And in the bootstrap.yml file, I will simply have something like this... which will only allow the maximum file size of 2 megabytes.

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

and finally, in my HTML file, I will have something like this...

<form id="profileForm"
   th:action="@{/update}"
   th:object="${user}"
   method="post" enctype="multipart/form-data">

   <!-- This will modify the method to PUT to match backend -->
   <input type="hidden" name="_method" value="PUT">

   ...

   <div class="col-md-3">
      <div class="form-group row">
         <label for="file" class="custom-file">
           <input name="file" type="file" id="file" aria-describedby="fileHelpId"  class="form-control-file btn btn-outline-info">
          </label>
          <small id="fileHelpId" class="form-text text-muted">Maximum size should be 2MB</small>
       </div>
   </div>

And this should work perfectly fine. I am also using Spring Boot 2.0.3

Knight Rider
  • 972
  • 2
  • 10
  • 22
  • Is there anything different in yours compared to mine that could be causing this error? Besides the applications.yml / applications.properties file. – Ryan Fasching Jul 19 '18 at 16:12
  • No there isn’t but I do know that yml files takes precedence when compared to properties. I was having the same problem but when I changed it, it worked for me so I shared. – Knight Rider Jul 19 '18 at 22:22
  • hmmmm I changed it too and it still gives me the error – Ryan Fasching Jul 20 '18 at 18:37
0

Did you try this ?

spring.http.multipart.maxFileSize = 20MB
spring.http.multipart.maxRequestSize = 20MB

OR

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

Works for me with Spring Boot 1.5.x

T.Rex
  • 111
  • 1
  • 13
  • Both notations configure the same properties- thanks to Spring's [Relaxed Binding](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding) – Peter Wippermann Mar 12 '19 at 11:21
0
This worked for me.
We can make changes to file size either in
1. application.properties 2. TOMCAT configuration(server.xml)

A. Ideally we should make use of application.properties 
B. And for that first we need to disable in TOMCAT config file "server.xml" by setting maxSwallowSize=-1 .

    <Connector port="8080" protocol="HTTP/1.1"
      connectionTimeout="20000"
      redirectPort="8443"
      maxSwallowSize = "-1"/>

C. Then set application.properties as

spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB
spring.servlet.multipart.enabled=true
Abdullah Imran
  • 259
  • 3
  • 13
0

Refer org.springframework.boot.autoconfigure.web.MutltipartProperties class to understand what prefix to use when setting the property. All the above naming convention works for different versions of springframework.

Reynadess
  • 1
  • 3