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