KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > files > FileUpload


1 package org.jahia.tools.files;
2
3 /*
4  * FileUpload
5  * Copyright (c) 2000 Jahia Ltd All rights reserved.
6  *
7  */

8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.servlet.ServletContext JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18
19 import com.oreilly.servlet.MultipartRequest;
20 import org.jahia.bin.Jahia;
21
22 /**
23  * Class FileUpload.
24  * Class to handle fileupload
25  * It's essentially a wrapper for the Multipart Request class
26  * of the com.oreilly.servlet.Multipart class
27  *
28  * @author Khue ng
29  * @version 1.0
30  */

31 public class FileUpload {
32
33     private static org.apache.log4j.Logger logger =
34         org.apache.log4j.Logger.getLogger (FileUpload.class);
35
36     private static final String JavaDoc REQ_CONTENT_TYPE = "multipart/form-data";
37
38     private MultipartRequest m_MultiPartReq = null;
39     private Hashtable JavaDoc m_QueryStringParams;
40     private ServletContext JavaDoc m_Context;
41     private HttpServletRequest JavaDoc m_Req;
42
43     private String JavaDoc m_SavePath = "";
44     private int m_FileMaxSize = 1024 * 1024; // 1 MB
45

46     /**
47      * Constructor
48      */

49     public FileUpload (ServletContext JavaDoc context, HttpServletRequest JavaDoc req)
50         throws IOException JavaDoc {
51
52         m_Context = context;
53         m_Req = req;
54         init();
55     }
56
57     /**
58      * Constructor
59      *
60      * @param context the servletContext object
61      * @param HttpservletRequest req
62      * @param HttpservletResponse res
63      * @param savePath the path where files should be saved
64      */

65     public FileUpload (ServletContext JavaDoc context, HttpServletRequest JavaDoc req,
66                        String JavaDoc savePath)
67         throws IOException JavaDoc {
68
69         m_Context = context;
70         m_Req = req;
71         m_SavePath = savePath;
72         init();
73
74     }
75
76     /**
77      * Constructor
78      *
79      * @param context the servletContext object
80      * @param HttpservletRequest req
81      * @param HttpservletResponse res
82      * @param savePath the path where files should be saved
83      * @param fileMaxSize the max size of file to upload
84      */

85     public FileUpload (ServletContext JavaDoc context, HttpServletRequest JavaDoc req,
86                        String JavaDoc savePath, int fileMaxSize)
87         throws IOException JavaDoc {
88
89         m_Context = context;
90         m_Req = req;
91         m_FileMaxSize = fileMaxSize;
92         m_SavePath = savePath;
93         init();
94
95     }
96
97     /**
98      * Init the MultiPartReq object if it's actually null
99      *
100      * @exception IOException
101      */

102     protected void init ()
103         throws IOException JavaDoc {
104
105         if (m_MultiPartReq == null) {
106
107             if (checkSavePath(m_SavePath)) {
108
109                 try {
110
111                     String JavaDoc encoding = "ISO-8859-1";
112                     if (m_Req.getCharacterEncoding() == null) {
113                         if (Jahia.getSettings() != null) {
114                             if (Jahia.getSettings().isUtf8Encoding()) {
115                                 encoding = "UTF-8";
116                             } else {
117                                 encoding = "ISO-8859-1";
118                             }
119                         }
120                     } else {
121                         encoding = m_Req.getCharacterEncoding();
122                     }
123
124                     m_MultiPartReq = new MultipartRequest(m_Req, m_SavePath,
125                         m_FileMaxSize, encoding);
126
127                 } catch (IOException JavaDoc ioe) {
128
129                     logger.error("Error while initializing FileUpload class:", ioe);
130                     throw new IOException JavaDoc(ioe.getMessage());
131                 }
132
133             } else {
134                 logger.error(
135                     "FileUpload::init storage path does not exists or can write");
136                 throw new IOException JavaDoc(
137                     "FileUpload::init storage path does not exists or cannot write");
138             }
139         }
140
141         parseQueryString();
142
143     }
144
145     /**
146      * Method getFiles()
147      *
148      * @return a Vector of uploaded files
149      * @exception IOException
150      */

151     public Vector JavaDoc getFiles ()
152         throws IOException JavaDoc {
153
154         logger.debug("fileMaxSize = " + m_FileMaxSize + ", SavePath = " + m_SavePath);
155
156         Enumeration JavaDoc fileNames = m_MultiPartReq.getFileNames();
157
158         File JavaDoc tmpFile = null;
159         Vector JavaDoc files = new Vector JavaDoc();
160
161         while (fileNames.hasMoreElements()) {
162             tmpFile = m_MultiPartReq.getFile( (String JavaDoc) fileNames.nextElement());
163             if (tmpFile != null) {
164                 files.addElement(tmpFile);
165             }
166         }
167
168         return files;
169     }
170
171     /**
172      * Return an enumeration of the filenames
173      *
174      * @return a Enumeration of filenames
175      * @exception IOException
176      */

177     public Enumeration JavaDoc getFileNames () {
178
179         return m_MultiPartReq.getFileNames();
180     }
181
182     /**
183      * Return a file object
184      *
185      * @param filename the name of the file
186      * @return a File object
187      */

188     public File JavaDoc getFile (String JavaDoc filename) {
189
190         return m_MultiPartReq.getFile(filename);
191     }
192
193     /**
194      * Return a file contentType
195      *
196      * @param filename the name of the file
197      * @return the Content Type as String
198      */

199     public String JavaDoc getFileContentType (String JavaDoc filename) {
200
201         return m_MultiPartReq.getContentType(filename);
202     }
203
204     /**
205      * Return a file SystemName ( the real name )
206      *
207      * @param filename the name of the file
208      * @return the ystem Name as String
209      */

210     public String JavaDoc getFileSystemName (String JavaDoc filename) {
211
212         return m_MultiPartReq.getFilesystemName(filename);
213     }
214
215     /**
216      * Return an enumeration of parameters name
217      *
218      * @return an enumeration of parameters name
219      */

220     public Enumeration JavaDoc getParameterNames () {
221
222         return m_MultiPartReq.getParameterNames();
223     }
224
225     /**
226      * Return the value of a parameter
227      *
228      * @param paramName the name of the parameter
229      * @return the value of a paramater as String
230      */

231     public String JavaDoc getParameter (String JavaDoc paramName) {
232
233         if (m_MultiPartReq.getParameter(paramName) == null) {
234             return (String JavaDoc) m_QueryStringParams.get(paramName);
235         }
236         return m_MultiPartReq.getParameter(paramName);
237     }
238
239     /**
240      * Return the value of a parameter
241      *
242      * @param paramName the name of the parameter
243      * @return the value of a paramater as String
244      */

245     public String JavaDoc getQueryParameter (String JavaDoc paramName) {
246
247         return (String JavaDoc) m_QueryStringParams.get(paramName);
248     }
249
250     /**
251      * Return the values of a parameter
252      *
253      * @param paramName the name of the parameter
254      * @return the values of a paramater as a String Array
255      */

256     public String JavaDoc[] getParameterValues (String JavaDoc paramName) {
257
258         return m_MultiPartReq.getParameterValues(paramName);
259     }
260
261     /**
262      * Method checkSavePath
263      * Check if the path where to save files is valid
264      *
265      * @param path the relative path where to save files to
266      * @return true if this directory exists, and can be write to
267      */

268     protected boolean checkSavePath (String JavaDoc path) {
269
270         if (path != null && (path.length() > 0)) {
271
272             logger.debug("path is " + path);
273             File JavaDoc tmpFile = new File JavaDoc(path);
274             if (tmpFile != null && tmpFile.isDirectory() && tmpFile.canWrite()) {
275                 m_SavePath = path;
276                 return true;
277             } else {
278                 return false;
279             }
280         } else {
281             return false;
282         }
283     }
284
285     /**
286      * delete temporary storage folder
287      */

288     public void deleteUploadFolder () {
289
290         File JavaDoc tmpDir = new File JavaDoc(m_SavePath);
291         if (tmpDir != null && tmpDir.isDirectory() && tmpDir.canWrite()) {
292             tmpDir.delete();
293         }
294     }
295
296     /**
297      * Parse a query String and create an hashtable of parameter/value
298      *
299      * @return java.lang.String
300      */

301     protected void parseQueryString () {
302
303         m_QueryStringParams = new Hashtable JavaDoc();
304         logger.debug(m_Req.getQueryString());
305
306         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(m_Req.getQueryString(),
307             "&");
308         Vector JavaDoc params = new Vector JavaDoc();
309         while (tokenizer.hasMoreTokens()) {
310             params.add(tokenizer.nextToken());
311         }
312
313         String JavaDoc param = null;
314         int size = params.size();
315         int pos = 0;
316         for (int i = 0; i < size; i++) {
317             param = (String JavaDoc) params.get(i);
318             pos = param.indexOf("=");
319             if (pos > 0) {
320                 m_QueryStringParams.put(param.substring(0, pos),
321                                         param.substring(pos + 1, param.length()));
322             }
323
324         }
325
326     }
327
328 }
Popular Tags