Advertisement
GagleKas

FileUpload

Sep 17th, 2011
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.apache.tomcat.util.http.fileupload.FileItemIterator;
  8. import org.apache.tomcat.util.http.fileupload.FileItemStream;
  9. import org.apache.tomcat.util.http.fileupload.FileUploadException;
  10. import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
  11. import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
  12. import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
  13. import org.apache.tomcat.util.http.fileupload.util.Streams;
  14.  
  15. public class FileUpload{
  16.     private static final String ERROR_NULL = " parameter cannot be null.";
  17.     private static final String ERROR_NEGATIVE = " must be greater than 0.";
  18.    
  19.     public static final int SUCCESSFUL = 0;
  20.     public static final int NO_MULTIPART_REQUEST = 1;
  21.     public static final int ERROR_PROCESSING_REQUEST = 2;
  22.     public static final int ERROR_LOCATION = 3;
  23.     public static final int ERROR_MAX_REQUEST_SIZE = 4;
  24.     public static final int ERROR_MAX_FILE_SIZE = 5;
  25.    
  26.     private static final String ENCODING = "UTF-8";
  27.    
  28.     private File location;
  29.     private int fileSizeThreshold;
  30.     private int maxFileSize;
  31.     private int maxRequestSize;
  32.    
  33.     private DiskFileItemFactory factory;
  34.     private ServletFileUpload upload;
  35.    
  36.     private String encoding;
  37.     private int status;
  38.    
  39.     public static class Field{
  40.         public static final int SUCCESSFUL = 0;
  41.         public static final int ERROR_LOCATION = 1;
  42.         public static final int ERROR_MAX_FILE_SIZE = 2;
  43.        
  44.         private String name;
  45.         private String value;
  46.         private String contentType;
  47.         private boolean isFile;
  48.         private DiskFileItem tmpFile;
  49.         private int status;
  50.        
  51.         public String getName (){
  52.             return name;
  53.         }
  54.        
  55.         public String getValue (){
  56.             return value;
  57.         }
  58.        
  59.         public String getContentType (){
  60.             return contentType;
  61.         }
  62.        
  63.         public boolean isFile (){
  64.             return isFile;
  65.         }
  66.        
  67.         public int getStatus (){
  68.             return status;
  69.         }
  70.        
  71.         public boolean write (File fileName){
  72.             if (!isFile || tmpFile == null) return false;
  73.            
  74.             try{
  75.                 tmpFile.write (fileName);
  76.                 return true;
  77.             }catch (Exception e){
  78.                 return false;
  79.             }
  80.         }
  81.        
  82.         public void delete (){
  83.             if (!isFile || tmpFile == null) return;
  84.            
  85.             tmpFile.delete ();
  86.         }
  87.     }
  88.    
  89.     public static boolean testLocation (String location){
  90.         if (location == null) throw new IllegalArgumentException ("\"location\"" + ERROR_NULL);
  91.        
  92.         boolean ok = true;
  93.         File tmp = null;
  94.        
  95.         try{
  96.             tmp = new File (location, Calendar.getInstance ().getTime ().getTime () + ".tmp");
  97.             tmp.createNewFile ();
  98.         }catch (IOException e){
  99.             ok = false;
  100.         }finally{
  101.             tmp.delete ();
  102.         }
  103.        
  104.         return ok;
  105.     }
  106.    
  107.     public FileUpload (String location){
  108.         this (location, ENCODING);
  109.     }
  110.    
  111.     public FileUpload (String location, String encoding){
  112.         if (location == null) throw new IllegalArgumentException ("\"location\"" + ERROR_NULL);
  113.         if (encoding == null) throw new IllegalArgumentException ("\"encoding\"" + ERROR_NULL);
  114.  
  115.         this.location = new File (location);
  116.         fileSizeThreshold = -1;
  117.         maxFileSize = -1;
  118.         maxRequestSize = -1;
  119.         this.encoding = encoding;
  120.        
  121.         factory = new DiskFileItemFactory ();
  122.         factory.setRepository (this.location);
  123.        
  124.         upload = new ServletFileUpload (factory);
  125.         upload.setHeaderEncoding (encoding);
  126.     }
  127.    
  128.     public void setFileSizeThreshold (int fileSizeThreshold){
  129.         if (fileSizeThreshold <= 0) throw new IllegalArgumentException ("\"fileSizeThreshold\"" + ERROR_NEGATIVE);
  130.        
  131.         this.fileSizeThreshold = fileSizeThreshold;
  132.         factory.setSizeThreshold (fileSizeThreshold);
  133.     }
  134.    
  135.     public void setMaxFileSize (int maxFileSize){
  136.         if (maxFileSize <= 0) throw new IllegalArgumentException ("\"maxFileSize\"" + ERROR_NEGATIVE);
  137.        
  138.         this.maxFileSize = maxFileSize;
  139.     }
  140.    
  141.     public void setMaxRequestSize (int maxRequestSize){
  142.         if (maxRequestSize <= 0) throw new IllegalArgumentException ("\"maxRequestSize\"" + ERROR_NEGATIVE);
  143.        
  144.         this.maxRequestSize = maxRequestSize;
  145.         upload.setSizeMax (maxRequestSize);
  146.     }
  147.    
  148.     public File getLocation (){
  149.         return location;
  150.     }
  151.  
  152.     public int getFileSizeThreshold (){
  153.         return fileSizeThreshold;
  154.     }
  155.  
  156.     public int getMaxFileSize (){
  157.         return maxFileSize;
  158.     }
  159.  
  160.     public int getMaxRequestSize (){
  161.         return maxRequestSize;
  162.     }
  163.  
  164.     public int getStatus (){
  165.         return status;
  166.     }
  167.    
  168.     public ArrayList<Field> parseRequest (HttpServletRequest request){
  169.         if (request == null) throw new IllegalArgumentException ("\"request\"" + ERROR_NULL);
  170.        
  171.         status = SUCCESSFUL;
  172.         ArrayList<Field> items = new ArrayList<Field> ();
  173.        
  174.         if (!ServletFileUpload.isMultipartContent (request)){
  175.             status = NO_MULTIPART_REQUEST;
  176.             return items;
  177.         }
  178.        
  179.         if (maxRequestSize != -1 && request.getContentLength () > maxRequestSize){
  180.             status = ERROR_MAX_REQUEST_SIZE;
  181.             return items;
  182.         }
  183.        
  184.         try{
  185.             FileItemIterator iter = upload.getItemIterator (request);
  186.             while (iter.hasNext ()){
  187.                 FileItemStream item = iter.next ();
  188.                
  189.                 Field field = new Field ();
  190.                 field.name = item.getFieldName ();
  191.                 field.isFile = !item.isFormField ();
  192.                 field.status = Field.SUCCESSFUL;
  193.                
  194.                 InputStream in = item.openStream ();
  195.                
  196.                 if (!field.isFile){
  197.                     field.value = Streams.asString (in, encoding);
  198.                 }else{
  199.                     field.value = item.getName ();
  200.                     field.contentType = item.getContentType ();
  201.                     field.tmpFile = (DiskFileItem)upload.getFileItemFactory ().createItem (field.name, field.contentType, false, field.value);
  202.                    
  203.                     if (field.tmpFile == null){
  204.                         status = ERROR_LOCATION;
  205.                         field.status = Field.ERROR_LOCATION;
  206.                     }else{
  207.                         try{
  208.                             Streams.copy (in, field.tmpFile.getOutputStream (), true);
  209.                            
  210.                             if (maxFileSize != -1 && field.tmpFile.getSize () > maxFileSize){
  211.                                 field.tmpFile.delete ();
  212.                                 field.tmpFile = null;
  213.                                 status = ERROR_MAX_FILE_SIZE;
  214.                                 field.status = Field.ERROR_MAX_FILE_SIZE;
  215.                             }
  216.                         }catch (IOException e){
  217.                             status = ERROR_LOCATION;
  218.                             field.status = Field.ERROR_LOCATION;
  219.                         }
  220.                     }
  221.                 }
  222.                
  223.                 items.add (field);
  224.             }
  225.         }catch (FileUploadException e){
  226.             status = ERROR_PROCESSING_REQUEST;
  227.         }catch (IOException e){
  228.             e.printStackTrace ();
  229.             status = ERROR_PROCESSING_REQUEST;
  230.         }
  231.        
  232.         return items;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement