Reputation: 2434
I'm having issues handling fileupload exceptions with Spring.
I have the next multipartResolver:
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding(this.defaultEncoding);
commonsMultipartResolver.setMaxInMemorySize(this.maxInMemorySize);
commonsMultipartResolver.setMaxUploadSize(this.maxUploadSize);
commonsMultipartResolver.setMaxUploadSizePerFile(this.maxUploadSizePerFile);
return commonsMultipartResolver;
}
With the next constant values:
webmvc.multipart.maxInMemorySize=10485760 //10MB
webmvc.multipart.maxUploadSize=10485760 //10MB
webmvc.multipart.maxUploadSizePerFile=5242880 //5MB
I have the next GlobalExceptionHandler:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = MultipartException.class)
public String handleMultipartException(Exception ex, HttpServletRequest request) {
if (ex instanceof MultipartException) {
final MultipartException mEx = (MultipartException) ex;
if (ex.getCause() instanceof FileUploadBase.FileSizeLimitExceededException) {
final FileUploadBase.FileSizeLimitExceededException flEx = (FileUploadBase.FileSizeLimitExceededException) mEx.getCause();
request.setAttribute("size", flEx.getPermittedSize());
} else if (ex.getCause() instanceof FileUploadBase.SizeLimitExceededException) {
final FileUploadBase.SizeLimitExceededException flEx = (FileUploadBase.SizeLimitExceededException) mEx.getCause();
request.setAttribute("size", flEx.getPermittedSize());
} else {
request.setAttribute("error", ex.getMessage());
}
} else {
request.setAttribute("error", ex.getMessage());
}
return "forward:/errors.do";
}
}
If I upload a file under 5MB everything works fine, no exceptions fired and the file is uploaded properly.
If I upload a file between 5MB and 10MB, the exception is catched by my GlobalExceptionHandler and the result is shown in my page /errors
However, if I upload a file bigger than 10MB, my GlobalExceptionHandler catches the exception and forwards to /errors, but!, then GlobalExceptionHandler catches again the exception, and keeps doing it until the end of the times.
If I don't missundertand, it seems that my multipart content is processed over and over again by the CommonsMultipartResolver even after GlobalExceptionHandler captured the exception and tried to forward to a complete different page (I tried using redirect instead of forward and the issue remains).
Why doesn't it behave equally in both scenarios? and how can I solve this issue?
I've tried to set webmvc.multipart.maxInMemorySize=-1
and webmvc.multipart.maxUploadSize=-1
but then this behaviour ocurrs whenever I upload something bigger than 5MB.
Upvotes: 2
Views: 4436
Reputation: 8324
I think you need the an exception handler for MaxUploadSizeExceededException
@ExceptionHandler(MaxUploadSizeExceededException.class)
public String handleError(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) {
.......
}
You can see more here
https://www.mkyong.com/spring/spring-mvc-how-to-handle-max-upload-size-exceeded-exception/
Upvotes: 2