Spring upload file size limit error Spring upload file size limit error spring spring

Spring upload file size limit error


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

spring.servlet.multipart.max-file-size=128MBspring.servlet.multipart.max-request-size=128MBspring.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 = 20MBspring.http.multipart.max-request-size = 20MB


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

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

Spring Boot 2.x / above:

spring.servlet.multipart.max-file-size=10MBspring.servlet.multipart.max-request-size=10MB


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