title: How to set maximum file size based on file name using Apache File upload utils
body: <p>I have a requirement where I need to allow different maximum-file-sizes for different cases. Example: Allow 5 MB for resume, only 3 MB for transcripts.</p>
  
  <p>I am using the following code to upload the file using apache file upload utils.</p>
  
  <pre><code>        ServletFileUpload upload = new ServletFileUpload();
          upload.setSizeMax(500000000);
          upload.setProgressListener(aupl);
          FileItemIterator  iter = upload.getItemIterator(req);           
  
          while (iter.hasNext()) {
              FileItemStream item = iter.next();
              if (!item.isFormField()) {                  
                  form_name = item.getFieldName();        
          InputStream stream = item.openStream();     
          FileOutputStream fop = new FileOutputStream(new File(temp_location));
          Streams.copy(stream, fop, true);                
              }             
          }                
  </code></pre>
  
  <p>The only way I can find the name of the field is using item.getFieldName() and I can do that only after doing upload.getItemIterator, but setSizeMax(500..) has to be set on upload before upload.getItemIterator is called. </p>
  
  <p>Is there a work around for this problem? If there is no solution, can you suggest any other File Upload API which handles this problem.</p>
  
  <p>Thanks</p>