KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > UploadFile


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.web;
21
22 import org.apache.commons.fileupload.DiskFileUpload;
23 import org.apache.commons.fileupload.FileItem;
24 import org.apache.commons.fileupload.FileUploadException;
25 import org.apache.log4j.Logger;
26 import java.io.File JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import org.openi.application.Application;
31
32
33 /**
34  * @author paullucas <br>
35  * This class handles file upload request.
36  * <br>
37  * Revision :
38  * Uddhab Pant - 07/07/2005
39  * Moved file upload code from servlet to this class.
40  */

41 public class UploadFile {
42     private static Logger logger = Logger.getLogger(UploadFile.class);
43
44     public UploadFile() {
45     }
46
47     /**
48      * This method uploads files
49      * @param request HttpServletRequest
50      * @return String return uploaded file name
51      * @throws Exception
52      */

53     public String JavaDoc uploadFile(HttpServletRequest JavaDoc request)
54         throws Exception JavaDoc {
55         logger.debug("begining file upload");
56
57         List JavaDoc fileItems = null;
58
59         try {
60             DiskFileUpload fu = new DiskFileUpload();
61             // maximum size before a FileUploadException will be thrown
62
fu.setSizeMax(Application.getInstance().getMaxFileUploadSize());
63             // maximum size that will be stored in memory
64
fu.setSizeThreshold(4096);
65             // the location for saving data that is larger than getSizeThreshold()
66
fu.setRepositoryPath(System.getProperty("java.io.tmpdir"));
67
68             fileItems = fu.parseRequest(request);
69         } catch (FileUploadException e) {
70             logger.error(e);
71             throw e;
72         }
73
74         // find the uploaded file, and the target directory
75
FileItem uploadedFile = null;
76         String JavaDoc targetDirectory = null;
77         logger.debug("found " + fileItems.size() + " items in request");
78
79         Iterator JavaDoc items = fileItems.iterator();
80
81         while (items.hasNext()) {
82             FileItem fileItem = (FileItem) items.next();
83
84             if ("uploadFile".equals(fileItem.getFieldName())) {
85                 uploadedFile = fileItem;
86             } else if ("targetDirectory".equals(fileItem.getFieldName())) {
87                 targetDirectory = fileItem.getString();
88             }
89         }
90         String JavaDoc fileName = "";
91         try {
92             fileName = parseFilename(uploadedFile.getName());
93             logger.debug("fileName: " + fileName);
94             logger.debug("saving uploadFile to directory: " + targetDirectory);
95             logger.debug("size: " + uploadedFile.getSize());
96
97             File JavaDoc newFile = new File JavaDoc(targetDirectory + "/" + fileName);
98             logger.info("saving file: " + newFile.getCanonicalPath());
99             uploadedFile.write(newFile);
100         } catch (Exception JavaDoc e) {
101             // evil - fileItem.write throws "Exception"!
102
logger.error(e);
103             throw e;
104         }
105         return targetDirectory + "/" + fileName;
106     }
107
108     /**
109      * needed to support IE problems - when uploading a file in IE,
110      * the entire filename including full canonical path is used.
111      */

112     private String JavaDoc parseFilename(String JavaDoc inputName) {
113         String JavaDoc cleanFilename = inputName;
114         int idx = cleanFilename.lastIndexOf('/');
115
116         if ((idx >= 0) && (idx < cleanFilename.length())) {
117             cleanFilename = cleanFilename.substring(idx + 1);
118         }
119
120         idx = cleanFilename.lastIndexOf('\\');
121
122         if ((idx >= 0) && (idx < cleanFilename.length())) {
123             cleanFilename = cleanFilename.substring(idx + 1);
124         }
125
126         return cleanFilename;
127     }
128 }
129
Popular Tags