title: How to nicely handle file upload MaxUploadSizeExceededException with Spring Security
body: <p>I'm using Spring Web 4.0.5, Spring Security 3.2.4, Commons FileUpload 1.3.1, Tomcat 7 and I'm getting an ugly <code>MaxUploadSizeExceededException</code> when my upload size limit is exceeded, which results in a "500 Internal Server Error". I handle it with a nice generic popup, but I'd rather have my Controller take care of it by going back to the originating form with the proper explanation message.</p>
  
  <p>I've seen a similar question asked many times, with a few solutions that might work when not using Spring Security; none of the ones I tried worked for me.</p>
  
  <p>The problem might be that when using Spring Security, the <code>CommonsMultipartResolver</code> is not added as a "multipartResolver" bean but as a "filterMultipartResolver":</p>
  
  <pre><code>@Bean(name="filterMultipartResolver")
  CommonsMultipartResolver filterMultipartResolver() {
      CommonsMultipartResolver filterMultipartResolver = new CommonsMultipartResolver();
      filterMultipartResolver.setMaxUploadSize(MAXSIZE);
      return filterMultipartResolver;
  }
  </code></pre>
  
  <p>If I set <code>filterMultipartResolver.setResolveLazily(true);</code> it makes no difference.</p>
  
  <p>If I subclass the <code>CommonsMultipartResolver</code> with my own and override the <code>parseRequest()</code> method with something that traps the <code>MaxUploadSizeExceededException</code> and returns an empty <code>MultipartParsingResult</code>, I get a "403 Forbidden" error:</p>
  
  <pre><code>public class ExtendedCommonsMultipartResolver extends CommonsMultipartResolver {
      protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
          String encoding = determineEncoding(request);
          try {
              return super.parseRequest(request);
          } catch (MaxUploadSizeExceededException e) {
              return parseFileItems(Collections.&lt;FileItem&gt; emptyList(), encoding);
          }
      }
  }
  </code></pre>
  
  <p>Finally, there's no point in implementing some kind of local or global <code>ExceptionHandler</code> because it is never called. </p>
  
  <p>If I don't find a better solution, I'll just remove the upload size limit and handle it myself in the controller, with the drawback of having the user wait until the upload is finished before seeing the error message about file size.
  Of I might even ignore all of this because, being it an image in this case, I could just resize it down to proper values.</p>
  
  <p>Still, I'd like to see a solution to this problem.</p>
  
  <p>Thank you</p>
  
  <p>EDIT:</p>
  
  <p>I add the stack trace as requested. This is the case where a 500 is generated.</p>
  
  <pre><code>May 30, 2014 12:47:17 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/site] threw exception
  org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 1000000 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3403852) exceeds the configured maximum (1000000)
      at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:162)
      at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:142)
      at org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:110)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3403852) exceeds the configured maximum (1000000)
      at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.&lt;init&gt;(FileUploadBase.java:965)
      at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
      at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
      at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
      at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:158)
      ... 19 more
  </code></pre>