1 40 package org.dspace.app.webui.util; 41 42 import java.io.File ; 43 import java.io.IOException ; 44 import java.util.Collection ; 45 import java.util.Collections ; 46 import java.util.Enumeration ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Map ; 51 import java.util.Vector ; 52 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletRequestWrapper ; 55 56 import org.apache.commons.fileupload.DiskFileUpload; 57 import org.apache.commons.fileupload.FileItem; 58 import org.dspace.core.ConfigurationManager; 59 60 69 public class FileUploadRequest extends HttpServletRequestWrapper 70 { 71 72 73 private List items = null; 74 75 private HashMap parameters = new HashMap (); 76 77 private HashMap fileitems = new HashMap (); 78 79 private Vector filenames = new Vector (); 80 81 private String tempDir = null; 82 83 84 private HttpServletRequest original = null; 85 86 92 public FileUploadRequest(HttpServletRequest req) throws IOException 93 { 94 super(req); 95 96 original = req; 97 98 tempDir = ConfigurationManager.getProperty("upload.temp.dir"); 99 int maxSize = ConfigurationManager.getIntProperty("upload.max"); 100 101 DiskFileUpload upload = new DiskFileUpload(); 102 103 try 104 { 105 upload.setRepositoryPath(tempDir); 106 upload.setSizeMax(maxSize); 107 items = upload.parseRequest(req); 108 for (Iterator i = items.iterator(); i.hasNext();) 109 { 110 FileItem item = (FileItem) i.next(); 111 if (item.isFormField()) 112 { 113 parameters 114 .put(item.getFieldName(), item.getString("UTF-8")); 115 } 116 else 117 { 118 parameters.put(item.getFieldName(), item.getName()); 119 fileitems.put(item.getFieldName(), item); 120 filenames.add(item.getName()); 121 122 String filename = getFilename(item.getName()); 123 if (filename != null && !"".equals(filename)) 124 { 125 item.write(new File (tempDir + File.separator 126 + filename)); 127 } 128 } 129 } 130 } 131 catch (Exception e) 132 { 133 IOException t = new IOException (e.getMessage()); 134 t.initCause(e); 135 throw t; 136 } 137 } 138 139 public Enumeration getParameterNames() 141 { 142 Collection c = parameters.keySet(); 143 return Collections.enumeration(c); 144 } 145 146 public String getParameter(String name) 147 { 148 return (String ) parameters.get(name); 149 } 150 151 public String [] getParameterValues(String name) 152 { 153 return (String []) parameters.values().toArray(); 154 } 155 156 public Map getParameterMap() 157 { 158 Map map = new HashMap (); 159 Enumeration eNum = getParameterNames(); 160 161 while (eNum.hasMoreElements()) 162 { 163 String name = (String ) eNum.nextElement(); 164 map.put(name, getParameterValues(name)); 165 } 166 167 return map; 168 } 169 170 public String getFilesystemName(String name) 171 { 172 String filename = getFilename(((FileItem) fileitems.get(name)) 173 .getName()); 174 return tempDir + File.separator + filename; 175 } 176 177 public String getContentType(String name) 178 { 179 return ((FileItem) fileitems.get(name)).getContentType(); 180 } 181 182 public File getFile(String name) 183 { 184 String filename = getFilename(((FileItem) fileitems.get(name)) 185 .getName()); 186 if ("".equals(filename.trim())) 187 { 188 return null; 189 } 190 return new File (tempDir + File.separator + filename); 191 } 192 193 public Enumeration getFileNames() 194 { 195 return filenames.elements(); 196 } 197 198 203 public HttpServletRequest getOriginalRequest() 204 { 205 return original; 206 } 207 208 private String getFilename(String filepath) 211 { 212 String filename = filepath.trim(); 213 214 int index = filepath.lastIndexOf(File.separator); 215 if (index > -1) 216 { 217 filename = filepath.substring(index); 218 } 219 return filename; 220 } 221 } | Popular Tags |