Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Calendar;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.tomcat.util.http.fileupload.FileItemIterator;
- import org.apache.tomcat.util.http.fileupload.FileItemStream;
- import org.apache.tomcat.util.http.fileupload.FileUploadException;
- import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
- import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
- import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
- import org.apache.tomcat.util.http.fileupload.util.Streams;
- public class FileUpload{
- private static final String ERROR_NULL = " parameter cannot be null.";
- private static final String ERROR_NEGATIVE = " must be greater than 0.";
- public static final int SUCCESSFUL = 0;
- public static final int NO_MULTIPART_REQUEST = 1;
- public static final int ERROR_PROCESSING_REQUEST = 2;
- public static final int ERROR_LOCATION = 3;
- public static final int ERROR_MAX_REQUEST_SIZE = 4;
- public static final int ERROR_MAX_FILE_SIZE = 5;
- private static final String ENCODING = "UTF-8";
- private File location;
- private int fileSizeThreshold;
- private int maxFileSize;
- private int maxRequestSize;
- private DiskFileItemFactory factory;
- private ServletFileUpload upload;
- private String encoding;
- private int status;
- public static class Field{
- public static final int SUCCESSFUL = 0;
- public static final int ERROR_LOCATION = 1;
- public static final int ERROR_MAX_FILE_SIZE = 2;
- private String name;
- private String value;
- private String contentType;
- private boolean isFile;
- private DiskFileItem tmpFile;
- private int status;
- public String getName (){
- return name;
- }
- public String getValue (){
- return value;
- }
- public String getContentType (){
- return contentType;
- }
- public boolean isFile (){
- return isFile;
- }
- public int getStatus (){
- return status;
- }
- public boolean write (File fileName){
- if (!isFile || tmpFile == null) return false;
- try{
- tmpFile.write (fileName);
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public void delete (){
- if (!isFile || tmpFile == null) return;
- tmpFile.delete ();
- }
- }
- public static boolean testLocation (String location){
- if (location == null) throw new IllegalArgumentException ("\"location\"" + ERROR_NULL);
- boolean ok = true;
- File tmp = null;
- try{
- tmp = new File (location, Calendar.getInstance ().getTime ().getTime () + ".tmp");
- tmp.createNewFile ();
- }catch (IOException e){
- ok = false;
- }finally{
- tmp.delete ();
- }
- return ok;
- }
- public FileUpload (String location){
- this (location, ENCODING);
- }
- public FileUpload (String location, String encoding){
- if (location == null) throw new IllegalArgumentException ("\"location\"" + ERROR_NULL);
- if (encoding == null) throw new IllegalArgumentException ("\"encoding\"" + ERROR_NULL);
- this.location = new File (location);
- fileSizeThreshold = -1;
- maxFileSize = -1;
- maxRequestSize = -1;
- this.encoding = encoding;
- factory = new DiskFileItemFactory ();
- factory.setRepository (this.location);
- upload = new ServletFileUpload (factory);
- upload.setHeaderEncoding (encoding);
- }
- public void setFileSizeThreshold (int fileSizeThreshold){
- if (fileSizeThreshold <= 0) throw new IllegalArgumentException ("\"fileSizeThreshold\"" + ERROR_NEGATIVE);
- this.fileSizeThreshold = fileSizeThreshold;
- factory.setSizeThreshold (fileSizeThreshold);
- }
- public void setMaxFileSize (int maxFileSize){
- if (maxFileSize <= 0) throw new IllegalArgumentException ("\"maxFileSize\"" + ERROR_NEGATIVE);
- this.maxFileSize = maxFileSize;
- }
- public void setMaxRequestSize (int maxRequestSize){
- if (maxRequestSize <= 0) throw new IllegalArgumentException ("\"maxRequestSize\"" + ERROR_NEGATIVE);
- this.maxRequestSize = maxRequestSize;
- upload.setSizeMax (maxRequestSize);
- }
- public File getLocation (){
- return location;
- }
- public int getFileSizeThreshold (){
- return fileSizeThreshold;
- }
- public int getMaxFileSize (){
- return maxFileSize;
- }
- public int getMaxRequestSize (){
- return maxRequestSize;
- }
- public int getStatus (){
- return status;
- }
- public ArrayList<Field> parseRequest (HttpServletRequest request){
- if (request == null) throw new IllegalArgumentException ("\"request\"" + ERROR_NULL);
- status = SUCCESSFUL;
- ArrayList<Field> items = new ArrayList<Field> ();
- if (!ServletFileUpload.isMultipartContent (request)){
- status = NO_MULTIPART_REQUEST;
- return items;
- }
- if (maxRequestSize != -1 && request.getContentLength () > maxRequestSize){
- status = ERROR_MAX_REQUEST_SIZE;
- return items;
- }
- try{
- FileItemIterator iter = upload.getItemIterator (request);
- while (iter.hasNext ()){
- FileItemStream item = iter.next ();
- Field field = new Field ();
- field.name = item.getFieldName ();
- field.isFile = !item.isFormField ();
- field.status = Field.SUCCESSFUL;
- InputStream in = item.openStream ();
- if (!field.isFile){
- field.value = Streams.asString (in, encoding);
- }else{
- field.value = item.getName ();
- field.contentType = item.getContentType ();
- field.tmpFile = (DiskFileItem)upload.getFileItemFactory ().createItem (field.name, field.contentType, false, field.value);
- if (field.tmpFile == null){
- status = ERROR_LOCATION;
- field.status = Field.ERROR_LOCATION;
- }else{
- try{
- Streams.copy (in, field.tmpFile.getOutputStream (), true);
- if (maxFileSize != -1 && field.tmpFile.getSize () > maxFileSize){
- field.tmpFile.delete ();
- field.tmpFile = null;
- status = ERROR_MAX_FILE_SIZE;
- field.status = Field.ERROR_MAX_FILE_SIZE;
- }
- }catch (IOException e){
- status = ERROR_LOCATION;
- field.status = Field.ERROR_LOCATION;
- }
- }
- }
- items.add (field);
- }
- }catch (FileUploadException e){
- status = ERROR_PROCESSING_REQUEST;
- }catch (IOException e){
- e.printStackTrace ();
- status = ERROR_PROCESSING_REQUEST;
- }
- return items;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement