Catch Spring Error MVP Maxupload

I know that it is difficult to check the file size on the client (browser) side only with pure javascript.

Now, my question is: is there a way on the server side to catch such an exception?

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 2000000 bytes

What happens is that it does not reach my mail method @controller, and it just throws an exception that catches up with my error.jsp.

What I was thinking about is it possible to do this in the annotated spring mvc method?

@RequestMapping("/uploadFile.htm")
    public String uploadAttachment(
        HttpServletRequest request,
        @RequestParam(required = false, value = "attached-file") MultipartFile file,
        ModelMap model) throws Exception {

        if(checkfilesize(file)){
            //populate model
            //add error if appplicable
            //return same form again
        }
        //return success
    }       
}

My problem is that it does not reach this point and just throws a big fat exception.

Although error.jsp was able to catch it, I would think that it is very user friendly if I can warn the user that the file they are loading is exceeding the limit.

This is spring application MVC 2.5. Is it possible?

+5
2

DispatcherServlet.doDispatch(), , HandlerExceptionResolver .

+4

maxUploadSize /, :

if (file.getSize() > 2000000) {
    result.rejectValue("file", "<your.message.key>");
}

, , CommonsMultipartResolver maxUploadSize.

+3

All Articles