1 23 24 package org.infoglue.cms.util; 25 26 import java.io.File ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.servlet.http.HttpServletRequest ; 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 47 48 public class FileUploadHelper 49 { 50 private final static Logger logger = Logger.getLogger(FileUploadHelper.class.getName()); 51 52 public FileUploadHelper() 53 { 54 } 56 57 public static File getUploadedFile(MultiPartRequestWrapper mpr) 58 { 59 File renamedFile = null; 60 61 try 62 { 63 if(mpr != null) 64 { 65 Enumeration names = mpr.getFileNames(); 66 while (names.hasMoreElements()) 67 { 68 String name = (String )names.nextElement(); 69 70 File file = mpr.getFile(name); 71 logger.info("file:" + file.getPath() + ":" + file.exists()); 72 if(file != null) 73 { 74 String fileSystemName = mpr.getFilesystemName(name); 76 77 String fileName = "Import_" + System.currentTimeMillis() + fileSystemName; 78 fileName = new VisualFormatter().replaceNonAscii(fileName, '_'); 79 80 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 81 fileSystemName = filePath + File.separator + fileName; 82 83 renamedFile = new File (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 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 106 107 public static void listMultiPartFiles(HttpServletRequest req) 108 { 109 try 110 { 111 File tempDir = new File ("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 )req)) 117 { 118 List fileItems = upload.parseRequest((HttpServletRequest )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 i = fileItems.iterator(); 125 while(i.hasNext()) 126 { 127 Object 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 fieldName = dfi.getFieldName(); 134 String fileName = dfi.getName(); 135 String 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 uploadedFile = new File ("c:/temp/uploads/" + fileName); 145 dfi.write(uploadedFile); 146 147 req.setAttribute(dfi.getFieldName(), uploadedFile.getAbsolutePath()); 148 } 149 150 } 151 } 152 } 153 catch (Exception e) 154 { 155 e.printStackTrace(); 156 } 157 } 158 } | Popular Tags |