title: Handling MaxUploadSizeExceededException can not stop uploading file
body: <p>I want to check size of uploading files and prevent files loaded in memory entirely. I'm using CommonsMultipartFile. The uploaded file will be processed and persisted in DB. AbstractCoupleUploadController class handles incoming request that containing files:</p>
  
  <pre><code>public abstract class AbstractCoupleUploadController&lt;T extends Serializable&gt; extends RemoteServiceServlet implements ServletContextAware,
          UploadServlet&lt;WorkshopHistoryModel&gt;
  {
      ...
  
      @RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
      public ModelAndView handleRequest(@RequestParam("firstFile") CommonsMultipartFile firstFile,
              @RequestParam("secondFile") CommonsMultipartFile secondFile, HttpServletRequest request, HttpServletResponse response)
      {
          synchronized(this)
          {
              initThreads();
              perThreadRequest.set(request);
              perThreadResponse.set(response);
          }
  
          handleUpload(firstFile,secondFile,request,response);
          response.getWriter().flush();
          response.flushBuffer();
          return null;
      }
  
      private void handleUpload(CommonsMultipartFile firstFile, CommonsMultipartFile secondFile, HttpServletRequest request,
          HttpServletResponse response) throws IOException
      {
          response.setContentType("text/html");
          if(firstFile.getSize() == 0 || secondFile.getSize() == 0)
          {
              response.getWriter().print(AppConstants.UPLOAD_ZERO_SIZE_FILE);
              return;
          }
  
          // other validations
          // uploading:
          try
          {
              String content = request.getParameter(CoupleUploadPanel.CONTENT);
              T model = deserialize(content);
              UploadResultModel resultModel = upload(model,firstFile,secondFile); // it's implemented in UploadFileServletImpl 
              if(resultModel.hasCriticalError())
              {
                  response.getWriter().print(AppConstants.UPLOAD_FAIL + "," + String.valueOf(resultModel.getWorkshopHistoryId()));
              }
              else
              {
                  response.getWriter().print(AppConstants.UPLOAD_SUCCESS + "," + String.valueOf(resultModel.getWorkshopHistoryId()));
              }
          }
          catch(ProcessRequestException e)
          {
             // write upload error description in response.getWriter()
          }
          catch(Exception e)
          {
              e.printStackTrace();
              response.getWriter().print(AppConstants.UPLOAD_UNKOWN_ERROR);
          }
      }
  
      ...
  }
  </code></pre>
  
  <p>I have a multipartResolver bean in my app-servlet.xml (file.upload.max_size=9437184), And also a maxUploadSizeExceededExceptionHandler bean for handling UploadSizeExceededExceptions:</p>
  
  <pre><code> &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
       &lt;property name="maxUploadSize" value="${file.upload.max_size}" /&gt;
   &lt;/bean&gt;
   &lt;bean id="maxUploadSizeExceededExceptionHandler" class="com.insurance.ui.server.uploadfile.MaxUploadSizeExceededExceptionHandler"&gt;
       &lt;property name="order" value="1"/&gt;
   &lt;/bean&gt;
  </code></pre>
  
  <p>My maxUploadSizeExceededExceptionHandler:</p>
  
  <pre><code>public class MaxUploadSizeExceededExceptionHandler implements HandlerExceptionResolver, Ordered
  {
      private int order;
  
      @Override
      public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      {
          if(ex instanceof MaxUploadSizeExceededException)
          {
              try
              {
                  response.getWriter().print(ErrorConstants.UPLOAD_SIZE_EXCEED + "," + (((MaxUploadSizeExceededException) ex).getMaxUploadSize()/(1024*1024)));
                  response.getWriter().flush();
                  response.flushBuffer();
                  return new ModelAndView();
              }
              catch(IOException e)
              {
              }
          }
          return null;
      }
      ...
  }
  </code></pre>
  
  <p>When i upload a very large file (more than ${file.upload.max_size}, about 700MB), CommonsMultipartResolver throws MaxUploadSizeExceededException immediately which I'm catching and handling it (writing in response.getWriter()) .But <strong>My problem: My browser upload-progress bar shows the file is still uploading!! Why?</strong></p>
  
  <p><strong>UPDATE:</strong> I'm using:</p>
  
  <ul>
  <li>Spring-*-3.0.5.RELEASE</li>
  <li>commons-fileupload-1.1.1</li>
  </ul>
  
  <p>and also tried:</p>
  
  <ul>
  <li>Spring-*-3.1.2.RELEASE</li>
  <li>commons-fileupload-1.3</li>
  </ul>
  
  <p>and my AS:</p>
  
  <ul>
  <li>Tomcat 6 (in development)</li>
  <li>Jboss 7 (in production)</li>
  </ul>
  
  <p><strong>UPDATE 2:</strong> In the client side, i'm using GWT (I think it doesn't matter):</p>
  
  <p>Uploading is started by clicking submitRequestButton:</p>
  
  <pre><code>@UiHandler("submitRequestButton")
  public void submitRequestButtonClick(ClickEvent event)
  {
      try
      {
          // some validation
          submitRequestButton.setEnabled(false);
          uploadPanel.upload(model.getWorkshopHistoryModel()); // uploadPanel is from the CoupleUploadPanel type
      }
      catch(ValidationException exception)
      {
          // handle validation errors
      }
      catch(SerializationException e)
      {
          // handle serialization errors
      }
  }
  </code></pre>
  
  <p>I have a CoupleUploadPanel Widget for uploading (two files):</p>
  
  <pre><code>public class CoupleUploadPanel&lt;T extends Serializable&gt; extends FormPanel
  {
      public final static String CONTENT = "content";
      private static final String FIRST_FILE = "firstFile";
      private static final String SECOND_FILE = "secondFile";
  
      private Hidden contentInput;
      private FileUpload firstFileUploadInput;
      private FileUpload secondFileUploadInput;
      private SerializationStreamFactory factory;
  
      public CoupleUploadPanel(UploadServletAsync&lt;T&gt; factory)
      {
          this(null,factory);
      }
  
      public CoupleUploadPanel(String url, UploadServletAsync&lt;T&gt; factory)
      {
          this.factory = (SerializationStreamFactory) factory;
          if(url != null)
          {
              setAction(url);
          }
          init();
      }
      public CoupleUploadPanel(String target, String url, UploadServletAsync&lt;T&gt; factory)
      {
          super(target);
          this.factory = (SerializationStreamFactory) factory;
          if(url != null)
          {
              setAction(url);
          }
          init();
      }
  
      private void init()
      {
          setMethod("POST");
          setEncoding(ENCODING_MULTIPART);
          firstFileUploadInput = new FileUpload();
          firstFileUploadInput.setName(CoupleUploadPanel.FIRST_FILE);
          secondFileUploadInput = new FileUpload();
          secondFileUploadInput.setName(CoupleUploadPanel.SECOND_FILE);
          contentInput = new Hidden();
          contentInput.setName(CONTENT);
          VerticalPanel panel = new VerticalPanel();
          panel.add(firstFileUploadInput);
          panel.add(secondFileUploadInput);
          panel.add(contentInput);
          add(panel);
      }
  
      public void upload(T input) throws SerializationException
      {
          contentInput.setValue(serialize(input));
          submit();
      }
  
      private String serialize(T input) throws SerializationException
      {
          SerializationStreamWriter writer = factory.createStreamWriter();
          writer.writeObject(input);
          return writer.toString();
      }
  }
  </code></pre>
  
  <p>We should pass a UploadServletAsync to CoupleUploadPanel constructor. UploadServletAsync and UploadServlet interfaces:</p>
  
  <pre><code>public interface UploadServletAsync&lt;T extends Serializable&gt; 
  {
      void upload(T model, AsyncCallback&lt;Void&gt; callback);
  }
  
  public interface UploadServlet&lt;T extends Serializable&gt; extends RemoteService
  {
      void upload(T model);
  }
  </code></pre>
  
  <p>So the <em>uploadPanel</em> will be instantiated in this way:</p>
  
  <pre><code>uploadPanel= new CoupleUploadPanel&lt;WorkshopHistoryModel&gt;((UploadFileServletAsync) GWT.create(UploadFileServlet.class));
  uploadPanel.setAction(UploadFileServlet.URL);
  </code></pre>
  
  <p>And a SubmitCompeleteHandler added to uploadPanel (<em>onSumbitComplete()</em> will be called when submit completed and results passed to the client side): </p>
  
  <pre><code>uploadPanel.addSubmitCompleteHandler(new SubmitCompleteHandler()
  {
  
      @Override
      public void onSubmitComplete(SubmitCompleteEvent event)
      {
          String s = event.getResults(); //contains whatever written by response.getWriter() 
          if(s == null)
          {
              // navigate to request list page
          }
          else
          {
              String[] response = s.split(",");
              // based on response: 
              // show error messages if any error occurred in file upload
              // else: navigate to upload result page
          }
      }
  });
  </code></pre>
  
  <p>UploadFileServlet and UploadFileServletAsync interfaces:</p>
  
  <pre><code>public interface UploadFileServlet extends UploadServlet&lt;WorkshopHistoryModel&gt;
  {
      String URL = "**/uploadFileService.mvc";
  }
  
  public interface UploadFileServletAsync extends UploadServletAsync&lt;WorkshopHistoryModel&gt;
  {
      public static final UploadFileServletAsync INSTANCE = GWT.create(UploadFileServlet.class);
  }
  </code></pre>
  
  <p>In the server side: the UploadFileServletImpl extends AbstractCoupleUploadController and implements the <em>upload()</em> method (upload process):</p>
  
  <pre><code>@RequestMapping(UploadFileServlet.URL)
  public class UploadFileServletImpl extends AbstractCoupleUploadController&lt;WorkshopHistoryModel&gt;
  {
      ...
  
      @Override
      protected UploadResultModel upload(WorkshopHistoryModel model, MultipartFile firstFile, MultipartFile secondFile)
              throws ProcessRequestException
      {
          return workshopHistoryService.submitList(model.getWorkshop(),firstFile,secondFile);
      }
  
      ...
  }
  </code></pre>