title: Handle FileUploadBase.SizeLimitExceededException with Spring 4
body: <p>I'm having issues handling fileupload exceptions with Spring.</p>
  
  <p>I have the next multipartResolver:</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>With the next constant values:</p>
  
  <pre><code>webmvc.multipart.maxInMemorySize=10485760 //10MB
  webmvc.multipart.maxUploadSize=10485760 //10MB
  webmvc.multipart.maxUploadSizePerFile=5242880 //5MB
  </code></pre>
  
  <p>I have the next GlobalExceptionHandler:</p>
  
  <pre><code>@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";
      }
  }
  </code></pre>
  
  <p>If I upload a file under 5MB everything works fine, no exceptions fired and the file is uploaded properly.</p>
  
  <p>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</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>Why doesn't it behave equally in both scenarios? and how can I solve this issue?</p>
  
  <p>I've tried to set <code>webmvc.multipart.maxInMemorySize=-1</code> and <code>webmvc.multipart.maxUploadSize=-1</code> but then this behaviour ocurrs whenever I upload something bigger than 5MB.</p>