KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > project > FileUploadServlet


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.project;
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.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33
34 /**
35  * @author paullucas
36  *
37  * TODO To change the template for this generated type comment go to
38  * Window - Preferences - Java - Code Style - Code Templates
39  */

40 public class FileUploadServlet extends HttpServlet JavaDoc {
41     private static Logger logger = Logger.getLogger(FileUploadServlet.class);
42
43     /**
44      *
45      */

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

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