When I try to upload a file, I get FileSizeLimitExceededException
exception and I can't handle it in my controller class.
My exception handler looks like the following:
@ExceptionHandler(Exception.class)
public ModelAndView handleFileSizeLimitExceeded(Exception exc) {
System.out.println("In Exception handler");
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("imageUploadMessage", "File too big");
modelAndView.addObject("currentUser", user);
modelAndView.setViewName("profile");
return modelAndView;
}
I have seen several similar questions on Stackoverflow
about this problem, but many of them don't have an accepted answer and the proposed answers there also could not solve my problem.
For example, I tried FileSizeLimitExceededException
, MultipartException
and MaxUploadSizeExceededException
in my exception handler, but the controller is never triggered at all.
Following the solution proposed here, I tried the following settings in my propertis
file:
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
server.tomcat.max-swallow-size=100MB
spring.servlet.multipart.resolve-lazily=true
But also no success.
Is there any solution to this problem?
In the meantime, I want to return a ModelAndView
as the return statement.
I hope somebody has found a real solution to this problem so far :).