How to set maximum file size based on file name using Apache File upload utils

12,128

If you instead loop through FileItem object instead of FileItemStream objects, all you need to do is set some constant max size values and compare each item to the appropriate value. If an item exceeds the size, handle it appropriately (throw new exception, trash the file, whatever you want to do), otherwise continue running as normal.

final long MAX_RESUME_SIZE = 5242880; // 5MB -> 5 * 1024 * 1024
final long MAX_TRANS_SIZE = 3145728; // 3MB -> 3 * 1024 * 1024

DiskFileItemFactory factory = new DiskFileItemFactory();
String fileDir = "your write-to location";
File dest = new File(fileDir);
if(!dest.isDirectory()){ dest.mkdir(); }
factory.setRepository(dest);
ServletFileUpload upload = new ServletFileUpload(factory);

for (FileItem item: upload.parseRequest(request)) { // request -> the HttpServletRequest
    if(!item.isFormField(){
        if(evaluateSize(item)){
            // handle as normal
        }else{
            // handle as too large
        }
    }
} // end while

private boolean evaluateSize(FileItem item){
    if(/* type is Resume */ && item.getSize() <= MAX_RESUME_SIZE){
        return true;
    }else if(/* type is Transcript */ && item.getSize() <= MAX_TRANS_SIZE){
        return true;
    }

    // assume too large
    return false;
}

Granted, you will have to add more logic if there is more than the two types of file, but you can see it is very simple to compare your file sizes before having to write.

Share:
12,128
Adithya Puram
Author by

Adithya Puram

Updated on June 04, 2022

Comments

  • Adithya Puram
    Adithya Puram almost 2 years

    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.

    I am using the following code to upload the file using apache file upload utils.

            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);                
                }             
            }                
    

    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.

    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.

    Thanks

    • Romain Hippeau
      Romain Hippeau almost 13 years
      Maybe you could use different servlets for each file type. Also 500000000 is 500MB - kind of large ?
    • shareef
      shareef over 7 years
      for testing purpose set the maxFileSize to more than 0 because i struggled to test it , assuming and setting it as 0 and when i put it 1 it worked
  • MJB
    MJB almost 13 years
    You could also count in the length of the formfields and add them to cumulativeSize as well....