View Javadoc

1   package com.atlassian.xwork;
2   
3   import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
4   import com.opensymphony.webwork.ServletActionContext;
5   
6   import java.io.File;
7   import java.util.*;
8   
9   import org.apache.log4j.Category;
10  
11  public class FileUploadUtils
12  {
13      private static Category log = Category.getInstance(FileUploadUtils.class);
14  
15      public static File getSingleFile() throws FileUploadException
16      {
17          FileUploadUtils.UploadedFile uploadedFile = getSingleUploadedFile();
18          return uploadedFile == null ? null : uploadedFile.getFile();
19      }
20  
21      public static UploadedFile getSingleUploadedFile() throws FileUploadException
22      {
23          MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
24  
25          FileUploadUtils.UploadedFile[] uploadedFiles = FileUploadUtils.handleFileUpload(multiWrapper, true);
26  
27          if (uploadedFiles.length == 0)
28              return null;
29  
30          return uploadedFiles[0];
31      }
32  
33      /**
34       * The multipart request should always be checked for errors before processing is done on it.
35       * @throws FileUploadException
36       */
37      public static void checkMultiPartRequestForErrors(MultiPartRequestWrapper multiWrapper) throws FileUploadException
38      {
39          if (multiWrapper.hasErrors())
40          {
41              FileUploadException fileUploadException = new FileUploadException();
42              Collection errors = multiWrapper.getErrors();
43              Iterator i = errors.iterator();
44  
45              while (i.hasNext())
46              {
47                  String error = (String)i.next();
48                  log.error(error);
49                  fileUploadException.addError(error);
50              }
51  
52              throw fileUploadException;
53          }
54      }
55  
56  
57      public static UploadedFile[] handleFileUpload(MultiPartRequestWrapper multiWrapper, boolean clean)
58          throws FileUploadException
59      {
60          checkMultiPartRequestForErrors(multiWrapper);
61  
62          Enumeration e = multiWrapper.getFileParameterNames();
63          List uploadedFiles = new ArrayList();
64  
65          while (e.hasMoreElements())
66          {
67              // get the value of this input tag
68              String inputValue = (String)e.nextElement();
69  
70              // Get a File object for the uploaded File
71              File[] files = multiWrapper.getFiles(inputValue);
72  
73              for (int i=0; i<files.length; i++) // support multiple upload controls with the same name
74              {
75                  File file = files[i];
76  
77                  // If it's null the upload failed
78                  if (file == null && !clean)
79                  {
80                      FileUploadException fileUploadException = new FileUploadException();
81                      fileUploadException.addError("Error uploading: " + multiWrapper.getFileSystemNames(inputValue)[i]);
82                      throw fileUploadException;
83                  }
84                  else if (file == null)
85                  {
86                      continue; //i.e. we simply don't store nulls in thye uploadedFiles array.
87                  }
88  
89                  UploadedFile uploadedFile = new UploadedFile(file, multiWrapper.getFileNames(inputValue)[i],
90                                                               multiWrapper.getContentTypes(inputValue)[i]);
91                  uploadedFiles.add(uploadedFile);
92              }
93          }
94  
95          return (UploadedFile[])uploadedFiles.toArray(new UploadedFile[0]);
96      }
97  
98      public static final class UploadedFile
99      {
100         private File file;
101         private String fileName;
102         private String contentType;
103 
104         public UploadedFile(File file, String fileName, String contentType)
105         {
106             this.file = file;
107             this.fileName = fileName;
108             this.contentType = contentType;
109         }
110 
111         public File getFile()
112         {
113             return file;
114         }
115 
116         public String getFileName()
117         {
118             return fileName;
119         }
120 
121         public String getContentType()
122         {
123             return contentType;
124         }
125     }
126 
127     public static final class FileUploadException extends Exception
128     {
129         private List errors = new ArrayList();
130 
131         public void addError(String error)
132         {
133             errors.add(error);
134         }
135 
136         public String[] getErrors()
137         {
138             return (String[])errors.toArray(new String[0]);
139         }
140 
141         public String getMessage()
142         {
143             String s = "";
144             String sep = "";
145             for (Iterator i = errors.iterator(); i.hasNext();)
146             {
147                 s += sep + i.next();
148                 sep = ", ";
149             }
150             return s;
151         }
152     }
153 }