0

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 :).

Update 1: Here is the screenshot of the exception: enter image description here

user1419243
  • 1,655
  • 3
  • 19
  • 33

2 Answers2

0

Define another class for global MaxUploadSizeExceededException handling. Below code is triggered only MaxUploadSizeExceededException exception thrown.

@ControllerAdvice
public class GlobalControllerAdvice {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView handleFileSizeLimitExceeded(HttpServletRequest req, Exception ex) {
        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;
    }
}
Ismail Durmaz
  • 2,521
  • 1
  • 6
  • 19
  • Unfortunately, your code doesn't work. I changed it to somehow and it seems better. But the only difference is that I don't get any error in the output of my eclipse. But it does NOT return to the current page and does not show the message I have set in the modelAndView. – user1419243 Dec 13 '20 at 13:53
  • hi, I have tested the code with ResponseEntity response. I saw the breakpoint is visited into the function. Did you try to put any breakpoints in the function? – Ismail Durmaz Dec 13 '20 at 14:20
  • I also got the function triggered when implemented using ControllerAdvice and if I set a breakpoint there, I also see that. You're right. But it seems that returning "ModelAndView" is not working properly there. I can't get redirected to the page I was there before. When I implement it this way, I don't get any exception in my eclipse and I don't return to the previous page, but I just see a "connection interrupted" page. – user1419243 Dec 13 '20 at 14:35
  • If you can return the code to the current page, could you please share your code with me. – user1419243 Dec 13 '20 at 14:36
0

To solve my problem, I just needed to update the verison of the Spring in my pom.xml file.

user1419243
  • 1,655
  • 3
  • 19
  • 33