1 18 19 20 package org.apache.struts.upload; 21 22 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.io.Serializable ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import org.apache.commons.fileupload.FileItem; 35 import org.apache.commons.fileupload.DiskFileUpload; 36 import org.apache.commons.fileupload.FileUploadException; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.apache.struts.action.ActionServlet; 40 import org.apache.struts.action.ActionMapping; 41 import org.apache.struts.config.ModuleConfig; 42 import org.apache.struts.Globals; 43 44 45 52 public class CommonsMultipartRequestHandler implements MultipartRequestHandler { 53 54 55 57 58 62 public static final long DEFAULT_SIZE_MAX = 250 * 1024 * 1024; 63 64 65 70 public static final int DEFAULT_SIZE_THRESHOLD = 256 * 1024; 71 72 73 75 76 79 protected static Log log = LogFactory.getLog( 80 CommonsMultipartRequestHandler.class); 81 82 83 86 private Hashtable elementsAll; 87 88 89 92 private Hashtable elementsFile; 93 94 95 98 private Hashtable elementsText; 99 100 101 104 private ActionMapping mapping; 105 106 107 110 private ActionServlet servlet; 111 112 113 115 116 121 public ActionServlet getServlet() { 122 return this.servlet; 123 } 124 125 126 131 public void setServlet(ActionServlet servlet) { 132 this.servlet = servlet; 133 } 134 135 136 141 public ActionMapping getMapping() { 142 return this.mapping; 143 } 144 145 146 151 public void setMapping(ActionMapping mapping) { 152 this.mapping = mapping; 153 } 154 155 156 166 public void handleRequest(HttpServletRequest request) 167 throws ServletException { 168 169 ModuleConfig ac = (ModuleConfig) request.getAttribute( 171 Globals.MODULE_KEY); 172 173 DiskFileUpload upload = new DiskFileUpload(); 175 upload.setHeaderEncoding(request.getCharacterEncoding()); 178 upload.setSizeMax(getSizeMax(ac)); 180 upload.setSizeThreshold((int) getSizeThreshold(ac)); 182 upload.setRepositoryPath(getRepositoryPath(ac)); 184 185 elementsText = new Hashtable (); 187 elementsFile = new Hashtable (); 188 elementsAll = new Hashtable (); 189 190 List items = null; 192 try { 193 items = upload.parseRequest(request); 194 } catch (DiskFileUpload.SizeLimitExceededException e) { 195 request.setAttribute( 197 MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED, 198 Boolean.TRUE); 199 return; 200 } catch (FileUploadException e) { 201 log.error("Failed to parse multipart request", e); 202 throw new ServletException (e); 203 } 204 205 Iterator iter = items.iterator(); 207 while (iter.hasNext()) { 208 FileItem item = (FileItem) iter.next(); 209 210 if (item.isFormField()) { 211 addTextParameter(request, item); 212 } else { 213 addFileParameter(item); 214 } 215 } 216 } 217 218 219 225 public Hashtable getTextElements() { 226 return this.elementsText; 227 } 228 229 230 236 public Hashtable getFileElements() { 237 return this.elementsFile; 238 } 239 240 241 246 public Hashtable getAllElements() { 247 return this.elementsAll; 248 } 249 250 251 254 public void rollback() { 255 Iterator iter = elementsFile.values().iterator(); 256 257 while (iter.hasNext()) { 258 FormFile formFile = (FormFile) iter.next(); 259 260 formFile.destroy(); 261 } 262 } 263 264 265 268 public void finish() { 269 rollback(); 270 } 271 272 273 275 276 284 protected long getSizeMax(ModuleConfig mc) { 285 return convertSizeToBytes( 286 mc.getControllerConfig().getMaxFileSize(), 287 DEFAULT_SIZE_MAX); 288 } 289 290 291 299 protected long getSizeThreshold(ModuleConfig mc) { 300 return convertSizeToBytes( 301 mc.getControllerConfig().getMemFileSize(), 302 DEFAULT_SIZE_THRESHOLD); 303 } 304 305 319 protected long convertSizeToBytes(String sizeString, long defaultSize) { 320 int multiplier = 1; 321 322 if (sizeString.endsWith("K")) { 323 multiplier = 1024; 324 } else if (sizeString.endsWith("M")) { 325 multiplier = 1024 * 1024; 326 } else if (sizeString.endsWith("G")) { 327 multiplier = 1024 * 1024 * 1024; 328 } 329 if (multiplier != 1) { 330 sizeString = sizeString.substring(0, sizeString.length() - 1); 331 } 332 333 long size = 0; 334 try { 335 size = Long.parseLong(sizeString); 336 } catch (NumberFormatException nfe) { 337 log.warn("Invalid format for file size ('" + sizeString + 338 "'). Using default."); 339 size = defaultSize; 340 multiplier = 1; 341 } 342 343 return (size * multiplier); 344 } 345 346 347 367 protected String getRepositoryPath(ModuleConfig mc) { 368 369 String tempDir = mc.getControllerConfig().getTempDir(); 371 372 if (tempDir == null || tempDir.length() == 0) { 374 if (servlet != null) { 375 ServletContext context = servlet.getServletContext(); 376 File tempDirFile = (File ) context.getAttribute( 377 "javax.servlet.context.tempdir"); 378 tempDir = tempDirFile.getAbsolutePath(); 379 } 380 381 if (tempDir == null || tempDir.length() == 0) { 383 tempDir = System.getProperty("java.io.tmpdir"); 384 } 385 } 386 387 if (log.isTraceEnabled()) { 388 log.trace("File upload temp dir: " + tempDir); 389 } 390 391 return tempDir; 392 } 393 394 395 404 protected void addTextParameter(HttpServletRequest request, FileItem item) { 405 String name = item.getFieldName(); 406 String value = null; 407 boolean haveValue = false; 408 String encoding = request.getCharacterEncoding(); 409 410 if (encoding != null) { 411 try { 412 value = item.getString(encoding); 413 haveValue = true; 414 } catch (Exception e) { 415 } 417 } 418 if (!haveValue) { 419 try { 420 value = item.getString("ISO-8859-1"); 421 } catch (java.io.UnsupportedEncodingException uee) { 422 value = item.getString(); 423 } 424 haveValue = true; 425 } 426 427 if (request instanceof MultipartRequestWrapper) { 428 MultipartRequestWrapper wrapper = (MultipartRequestWrapper) request; 429 wrapper.setParameter(name, value); 430 } 431 432 String [] oldArray = (String []) elementsText.get(name); 433 String [] newArray; 434 435 if (oldArray != null) { 436 newArray = new String [oldArray.length + 1]; 437 System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); 438 newArray[oldArray.length] = value; 439 } else { 440 newArray = new String [] { value }; 441 } 442 443 elementsText.put(name, newArray); 444 elementsAll.put(name, newArray); 445 } 446 447 448 454 protected void addFileParameter(FileItem item) { 455 FormFile formFile = new CommonsFormFile(item); 456 457 elementsFile.put(item.getFieldName(), formFile); 458 elementsAll.put(item.getFieldName(), formFile); 459 } 460 461 462 464 465 471 static class CommonsFormFile implements FormFile, Serializable { 472 473 476 FileItem fileItem; 477 478 479 485 public CommonsFormFile(FileItem fileItem) { 486 this.fileItem = fileItem; 487 } 488 489 490 495 public String getContentType() { 496 return fileItem.getContentType(); 497 } 498 499 500 507 public void setContentType(String contentType) { 508 throw new UnsupportedOperationException ( 509 "The setContentType() method is not supported."); 510 } 511 512 513 518 public int getFileSize() { 519 return (int)fileItem.getSize(); 520 } 521 522 523 530 public void setFileSize(int filesize) { 531 throw new UnsupportedOperationException ( 532 "The setFileSize() method is not supported."); 533 } 534 535 536 541 public String getFileName() { 542 return getBaseFileName(fileItem.getName()); 543 } 544 545 546 553 public void setFileName(String fileName) { 554 throw new UnsupportedOperationException ( 555 "The setFileName() method is not supported."); 556 } 557 558 559 572 public byte[] getFileData() throws FileNotFoundException , IOException { 573 return fileItem.get(); 574 } 575 576 577 584 public InputStream getInputStream() throws FileNotFoundException , IOException { 585 return fileItem.getInputStream(); 586 } 587 588 589 594 public void destroy() { 595 fileItem.delete(); 596 } 597 598 599 609 protected String getBaseFileName(String filePath) { 610 611 String fileName = new File (filePath).getName(); 613 614 int colonIndex = fileName.indexOf(":"); 616 if (colonIndex == -1) { 617 colonIndex = fileName.indexOf("\\\\"); 619 } 620 int backslashIndex = fileName.lastIndexOf("\\"); 621 622 if (colonIndex > -1 && backslashIndex > -1) { 623 fileName = fileName.substring(backslashIndex + 1); 626 } 627 628 return fileName; 629 } 630 631 636 public String toString() { 637 return getFileName(); 638 } 639 } 640 } 641 | Popular Tags |