KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > FileUploadHelper


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.io.File JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import org.apache.commons.fileupload.disk.DiskFileItem;
34 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
35 import org.apache.commons.fileupload.servlet.ServletFileUpload;
36 import org.apache.log4j.Logger;
37 import org.infoglue.cms.applications.common.VisualFormatter;
38
39 import webwork.multipart.MultiPartRequestWrapper;
40
41
42 /**
43   * This is the action-class for UpdateDigitalAssetVersion
44   *
45   * @author Mattias Bogeblad
46   */

47
48 public class FileUploadHelper
49 {
50     private final static Logger logger = Logger.getLogger(FileUploadHelper.class.getName());
51
52     public FileUploadHelper()
53     {
54         // don't instanciate, use static methods
55
}
56     
57     public static File JavaDoc getUploadedFile(MultiPartRequestWrapper mpr)
58     {
59         File JavaDoc renamedFile = null;
60         
61         try
62         {
63             if(mpr != null)
64             {
65                 Enumeration JavaDoc names = mpr.getFileNames();
66                 while (names.hasMoreElements())
67                 {
68                     String JavaDoc name = (String JavaDoc)names.nextElement();
69                                         
70                     File JavaDoc file = mpr.getFile(name);
71                     logger.info("file:" + file.getPath() + ":" + file.exists());
72                     if(file != null)
73                     {
74                         //String contentType = mpr.getContentType(name);
75
String JavaDoc fileSystemName = mpr.getFilesystemName(name);
76                         
77                         String JavaDoc fileName = "Import_" + System.currentTimeMillis() + fileSystemName;
78                         fileName = new VisualFormatter().replaceNonAscii(fileName, '_');
79                         
80                         String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
81                         fileSystemName = filePath + File.separator + fileName;
82                         
83                         renamedFile = new File JavaDoc(fileSystemName);
84                         boolean isRenamed = file.renameTo(renamedFile);
85                         logger.info("renamed file:" + renamedFile.getPath() + ":" + renamedFile.exists() + ":" + isRenamed);
86                     }
87                 }
88             }
89             else
90             {
91                 logger.error("File upload failed for some reason.");
92             }
93  
94         }
95         catch (Exception JavaDoc e)
96         {
97             logger.error("An error occurred when we get and rename an uploaded file:" + e.getMessage(), e);
98         }
99         
100         return renamedFile;
101     }
102     
103     /**
104      * Lists the files
105      */

106
107     public static void listMultiPartFiles(HttpServletRequest JavaDoc req)
108     {
109         try
110         {
111             File JavaDoc tempDir = new File JavaDoc("c:/temp/uploads");
112             System.out.println("tempDir:" + tempDir.exists());
113             
114             DiskFileItemFactory factory = new DiskFileItemFactory(1000, tempDir);
115             ServletFileUpload upload = new ServletFileUpload(factory);
116             if(ServletFileUpload.isMultipartContent((HttpServletRequest JavaDoc)req))
117             {
118                 List JavaDoc fileItems = upload.parseRequest((HttpServletRequest JavaDoc)req);
119                 System.out.println("******************************");
120                 System.out.println("fileItems:" + fileItems.size());
121                 System.out.println("******************************");
122                 req.setAttribute("Test", "Mattias Testar");
123                 
124                 Iterator JavaDoc i = fileItems.iterator();
125                 while(i.hasNext())
126                 {
127                     Object JavaDoc o = i.next();
128                     DiskFileItem dfi = (DiskFileItem)o;
129                     System.out.println("dfi:" + dfi.getFieldName());
130                     System.out.println("dfi:" + dfi);
131                     
132                     if (!dfi.isFormField()) {
133                         String JavaDoc fieldName = dfi.getFieldName();
134                         String JavaDoc fileName = dfi.getName();
135                         String JavaDoc contentType = dfi.getContentType();
136                         boolean isInMemory = dfi.isInMemory();
137                         long sizeInBytes = dfi.getSize();
138                         
139                         System.out.println("fieldName:" + fieldName);
140                         System.out.println("fileName:" + fileName);
141                         System.out.println("contentType:" + contentType);
142                         System.out.println("isInMemory:" + isInMemory);
143                         System.out.println("sizeInBytes:" + sizeInBytes);
144                         File JavaDoc uploadedFile = new File JavaDoc("c:/temp/uploads/" + fileName);
145                         dfi.write(uploadedFile);
146     
147                         req.setAttribute(dfi.getFieldName(), uploadedFile.getAbsolutePath());
148                     }
149                     
150                 }
151             }
152         }
153         catch (Exception JavaDoc e)
154         {
155             e.printStackTrace();
156         }
157     }
158 }
Popular Tags