The request was rejected because its size

2

I have the following code in my controller which is responsible for loading a client with a photo.

@RequestMapping(method = RequestMethod.POST, value = "/form")
public String guardar(@Valid @ModelAttribute("cliente") ClienteEntity cliente, BindingResult result, @RequestParam("file") MultipartFile foto, RedirectAttributes flash, SessionStatus sesion) { 


if (result.hasErrors() || foto.getSize() > 5242880L) {
            LOG.info("La foto pesa " + foto.getSize());
            return "form";
        } else {
             //...
        }

}

In my application.properties I have the following

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

So that you can not upload images with a weight greater than 5MB, but when I do I jump twice the same exception, which is the following

  

org.apache.tomcat.util.http.fileupload.FileUploadBase $ SizeLimitExceededException:   the request was rejected because its size (10506931) exceeds the   configured maximum (5242880)

I have tried using a try-catch and even the validation with the if but my program does not get to enter the controller and I can not do something to prevent the exception from happening.

EDIT

In this page I found the following code, I no longer skip the exception but it does not redirect me to my view error

@ControllerAdvice
public class GenericExceptionHandler {

    @ExceptionHandler(value = MultipartException.class)
    public ModelAndView handleFileUploadException(MultipartException mpex, HttpServletRequest request) {

        ModelAndView modelAndVew = new ModelAndView("error");
        modelAndVew.addObject("errorMsg", mpex.getMessage());
        return modelAndVew;
    }

In the console I get the warning of the size

  

2018-03-01 11: 45: 08.157 WARN 13880 --- [nio-8080-exec-4]   .m.m.a.ExceptionHandlerExceptionResolver: Resolved exception caused   by Handler execution:   org.springframework.web.multipart.MultipartException: Could not parse   multipart servlet request; nested exception is   java.lang.IllegalStateException:   org.apache.tomcat.util.http.fileupload.FileUploadBase $ SizeLimitExceededException:   the request was rejected because its size (10506931) exceeds the   configured maximum (5242880) 2018-03-01 11: 45: 09.148 WARN 13880 ---   [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver: Resolved   exception caused by Handler execution:   org.springframework.web.multipart.MultipartException: Could not parse   multipart servlet request; nested exception is   java.lang.IllegalStateException:   org.apache.tomcat.util.http.fileupload.FileUploadBase $ SizeLimitExceededException:   the request was rejected because its size (10506931) exceeds the   configured maximum (5242880)

And Chrome throws me an ERR_CONNECTION_RESET

Edit2

Add Apache Tomcat as a server since it had none selected.

In the multipart-config I have the following

<multipart-config>
      <!-- 50MB max -->
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>
      <file-size-threshold>0</file-size-threshold>
</multipart-config>

And in the controller add this again

if(foto.getSize() >= 5242880L) {
            LOG.error("---------------------- LA FOTO PESA DEMASIADO ----------------------");
            return LISTAR;
        }

But still it does not enter my if, so it does not address and I skip the ERR_CONNECTION_RESET from Chrome

    
asked by Lucas. D 01.03.2018 в 15:35
source

2 answers

1

I have managed to solve it: The answer was always before my eyes and in these two lines

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

If the size of the photo exceeds the size specified in spring.http.multipart.max-request-size it will display the ERR_CONNECTION_RESET and the rest of the code will stop running.

So I've changed the above for this

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

And now everything works correctly, as long as the photo is not bigger than 50MB or the specified value.

    
answered by 05.03.2018 / 18:55
source
1

The error that is being thrown occurs because the file exceeds the maximum size defined in the Tomcat configuration, so the request is not reaching the controller. You can edit the file webapps/manager/WEB-INF/web.xml , according to your needs, which is found in the directory where you have Tomcat installed. This file has the following section:

<multipart-config>
   <max-file-size>52428800</max-file-size>
   <max-request-size>52428800</max-request-size>
   <file-size-threshold>0<</file-size-threshold>
</multipart-config>

The values are expressed in bytes. In your case by the error message the file you were trying to upload weighed 10506931 bytes, so if you want the tomcat to allow its upload you should configure it with a higher value. Then the request would arrive at the controller spring, where the validation of the 5MB should fail.

    
answered by 05.03.2018 в 10:19