1 23 24 package org.dbforms.util; 25 26 import org.apache.commons.fileupload.DiskFileUpload; 27 import org.apache.commons.fileupload.FileItem; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 35 import java.util.Enumeration ; 36 import java.util.Hashtable ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Vector ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import org.dbforms.util.external.FileUtil; 43 44 45 80 public class MultipartRequest { 81 private static Log logCat = LogFactory.getLog(MultipartRequest.class); private Hashtable files = new Hashtable (); 84 private Hashtable parameters = new Hashtable (); 88 105 public MultipartRequest(HttpServletRequest request, 106 int maxPostSize) 107 throws IOException { 108 if (request == null) { 110 throw new IllegalArgumentException ("request cannot be null"); 111 } 112 113 DiskFileUpload upload = new DiskFileUpload(); 115 116 upload.setSizeMax(maxPostSize); 118 119 String tmpDir = System.getProperty("java.io.tmpdir"); 120 upload.setRepositoryPath(tmpDir); 121 122 try { 124 List items = upload.parseRequest(request); 125 Iterator iter = items.iterator(); 126 127 while (iter.hasNext()) { 128 FileItem item = (FileItem) iter.next(); 129 String name = item.getFieldName(); 130 131 if (item.isFormField()) { 132 String value = item.getString(); 133 134 Vector existingValues = (Vector ) parameters.get(name); 136 137 if (existingValues == null) { 138 existingValues = new Vector (); 139 parameters.put(name, existingValues); 140 } 141 142 existingValues.addElement(value); 143 } else { 144 String fileName = item.getName(); 145 146 if (!Util.isNull(fileName)) { 147 File f = new File (fileName); 148 fileName = FileUtil.filename(f.getName()); 151 152 FileHolder fileHolder = new FileHolder(fileName, item); 158 files.put(name, fileHolder); 159 logCat.info("buffered and now added as " + name 160 + " the following fileHolder:" 161 + fileHolder.getFileName()); 162 163 Vector existingValues = (Vector ) parameters.get(name); 170 171 if (existingValues == null) { 172 existingValues = new Vector (); 173 parameters.put(name, existingValues); 174 } 175 176 existingValues.addElement(fileName); 177 } 178 } 179 } 180 } catch (Exception e) { 181 logCat.error("MultipartRequest", e); 182 } 183 } 184 185 193 public String getContentType(String name) { 194 try { 195 FileHolder filePart = (FileHolder) files.get(name); 198 199 return filePart.getContentType(); } catch (Exception e) { 201 return null; 202 } 203 } 204 205 206 214 public FileHolder getFileHolder(String name) { 215 try { 216 return (FileHolder) files.get(name); 217 } catch (Exception e) { 218 return null; 219 } 220 } 221 222 223 231 public InputStream getFileInputStream(String name) { 232 try { 233 FileHolder filePart = (FileHolder) files.get(name); 236 237 return filePart.getInputStreamFromBuffer(); } catch (Exception e) { 239 return null; 240 } 241 } 242 243 244 251 public Enumeration getFileNames() { 252 return files.keys(); 253 } 254 255 256 265 public String getFilesystemName(String name) { 266 try { 267 FileHolder filePart = (FileHolder) files.get(name); 270 271 return filePart.getFileName(); } catch (Exception e) { 273 return null; 274 } 275 } 276 277 278 290 public String getParameter(String name) { 291 try { 292 Vector values = (Vector ) parameters.get(name); 293 294 if ((values == null) || (values.size() == 0)) { 295 return null; 296 } 297 298 String value = (String ) values.elementAt(0); 299 300 return value; 301 } catch (Exception e) { 302 return null; 303 } 304 } 305 306 307 313 public Enumeration getParameterNames() { 314 return parameters.keys(); 315 } 316 317 318 329 public String [] getParameterValues(String name) { 330 try { 331 Vector values = (Vector ) parameters.get(name); 332 333 if ((values == null) || (values.size() == 0)) { 334 return null; 335 } 336 337 String [] valuesArray = new String [values.size()]; 338 values.copyInto(valuesArray); 339 340 return valuesArray; 341 } catch (Exception e) { 342 return null; 343 } 344 } 345 } 346 | Popular Tags |