1 16 package net.myvietnam.mvncore.web.fileupload.disk; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedOutputStream ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.UnsupportedEncodingException ; 30 import java.rmi.server.UID ; 31 import java.util.Map ; 32 import org.apache.commons.io.IOUtils; 33 import org.apache.commons.io.FileCleaner; 34 import org.apache.commons.io.output.DeferredFileOutputStream; 35 36 import net.myvietnam.mvncore.web.fileupload.FileItem; 37 import net.myvietnam.mvncore.web.fileupload.FileUploadException; 38 import net.myvietnam.mvncore.web.fileupload.ParameterParser; 39 40 41 65 public class DiskFileItem 66 implements FileItem { 67 68 70 71 77 public static final String DEFAULT_CHARSET = "ISO-8859-1"; 78 79 80 82 83 86 private static final String UID = 87 new UID ().toString().replace(':', '_').replace('-', '_'); 88 89 92 private static int counter = 0; 93 94 95 98 private String fieldName; 99 100 101 105 private String contentType; 106 107 108 111 private boolean isFormField; 112 113 114 117 private String fileName; 118 119 120 123 private int sizeThreshold; 124 125 126 129 private File repository; 130 131 132 135 private byte[] cachedContent; 136 137 138 141 private transient DeferredFileOutputStream dfos; 142 143 146 private File dfosFile; 147 148 149 151 152 169 public DiskFileItem(String fieldName, String contentType, 170 boolean isFormField, String fileName, int sizeThreshold, 171 File repository) { 172 this.fieldName = fieldName; 173 this.contentType = contentType; 174 this.isFormField = isFormField; 175 this.fileName = fileName; 176 this.sizeThreshold = sizeThreshold; 177 this.repository = repository; 178 } 179 180 181 183 184 193 public InputStream getInputStream() 194 throws IOException { 195 if (!isInMemory()) { 196 return new FileInputStream (dfos.getFile()); 197 } 198 199 if (cachedContent == null) { 200 cachedContent = dfos.getData(); 201 } 202 return new ByteArrayInputStream (cachedContent); 203 } 204 205 206 213 public String getContentType() { 214 return contentType; 215 } 216 217 218 225 public String getCharSet() { 226 ParameterParser parser = new ParameterParser(); 227 parser.setLowerCaseNames(true); 228 Map params = parser.parse(getContentType(), ';'); 230 return (String ) params.get("charset"); 231 } 232 233 234 239 public String getName() { 240 return fileName; 241 } 242 243 244 246 247 254 public boolean isInMemory() { 255 if (cachedContent != null) { 256 return true; 257 } else { 258 return dfos.isInMemory(); 259 } 260 } 261 262 263 268 public long getSize() { 269 if (cachedContent != null) { 270 return cachedContent.length; 271 } else if (dfos.isInMemory()) { 272 return dfos.getData().length; 273 } else { 274 return dfos.getFile().length(); 275 } 276 } 277 278 279 286 public byte[] get() { 287 if (isInMemory()) { 288 if (cachedContent == null) { 289 cachedContent = dfos.getData(); 290 } 291 return cachedContent; 292 } 293 294 byte[] fileData = new byte[(int) getSize()]; 295 FileInputStream fis = null; 296 297 try { 298 fis = new FileInputStream (dfos.getFile()); 299 fis.read(fileData); 300 } catch (IOException e) { 301 fileData = null; 302 } finally { 303 if (fis != null) { 304 try { 305 fis.close(); 306 } catch (IOException e) { 307 } 309 } 310 } 311 312 return fileData; 313 } 314 315 316 328 public String getString(final String charset) 329 throws UnsupportedEncodingException { 330 return new String (get(), charset); 331 } 332 333 334 343 public String getString() { 344 byte[] rawdata = get(); 345 String charset = getCharSet(); 346 if (charset == null) { 347 charset = DEFAULT_CHARSET; 348 } 349 try { 350 return new String (rawdata, charset); 351 } catch (UnsupportedEncodingException e) { 352 return new String (rawdata); 353 } 354 } 355 356 357 377 public void write(File file) throws Exception { 378 if (isInMemory()) { 379 FileOutputStream fout = null; 380 try { 381 fout = new FileOutputStream (file); 382 fout.write(get()); 383 } finally { 384 if (fout != null) { 385 fout.close(); 386 } 387 } 388 } else { 389 File outputFile = getStoreLocation(); 390 if (outputFile != null) { 391 396 if (!outputFile.renameTo(file)) { 397 BufferedInputStream in = null; 398 BufferedOutputStream out = null; 399 try { 400 in = new BufferedInputStream ( 401 new FileInputStream (outputFile)); 402 out = new BufferedOutputStream ( 403 new FileOutputStream (file)); 404 IOUtils.copy(in, out); 405 } finally { 406 if (in != null) { 407 try { 408 in.close(); 409 } catch (IOException e) { 410 } 412 } 413 if (out != null) { 414 try { 415 out.close(); 416 } catch (IOException e) { 417 } 419 } 420 } 421 } 422 } else { 423 427 throw new FileUploadException( 428 "Cannot write uploaded file to disk!"); 429 } 430 } 431 } 432 433 434 441 public void delete() { 442 cachedContent = null; 443 File outputFile = getStoreLocation(); 444 if (outputFile != null && outputFile.exists()) { 445 outputFile.delete(); 446 } 447 } 448 449 450 459 public String getFieldName() { 460 return fieldName; 461 } 462 463 464 472 public void setFieldName(String fieldName) { 473 this.fieldName = fieldName; 474 } 475 476 477 487 public boolean isFormField() { 488 return isFormField; 489 } 490 491 492 502 public void setFormField(boolean state) { 503 isFormField = state; 504 } 505 506 507 516 public OutputStream getOutputStream() 517 throws IOException { 518 if (dfos == null) { 519 File outputFile = getTempFile(); 520 dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); 521 } 522 return dfos; 523 } 524 525 526 528 529 542 public File getStoreLocation() { 543 return dfos.getFile(); 544 } 545 546 547 549 550 553 protected void finalize() { 554 File outputFile = dfos.getFile(); 555 556 if (outputFile != null && outputFile.exists()) { 557 outputFile.delete(); 558 } 559 } 560 561 562 570 protected File getTempFile() { 571 File tempDir = repository; 572 if (tempDir == null) { 573 tempDir = new File (System.getProperty("java.io.tmpdir")); 574 } 575 576 String fileName = "upload_" + UID + "_" + getUniqueId() + ".tmp"; 577 578 File f = new File (tempDir, fileName); 579 FileCleaner.track(f, this); 580 return f; 581 } 582 583 584 586 587 593 private static String getUniqueId() { 594 final int limit = 100000000; 595 int current; 596 synchronized (DiskFileItem.class) { 597 current = counter++; 598 } 599 String id = Integer.toString(current); 600 601 if (current < limit) { 604 id = ("00000000" + id).substring(id.length()); 605 } 606 return id; 607 } 608 609 610 611 612 617 public String toString() { 618 return "name=" + this.getName() 619 + ", StoreLocation=" 620 + String.valueOf(this.getStoreLocation()) 621 + ", size=" 622 + this.getSize() 623 + "bytes, " 624 + "isFormField=" + isFormField() 625 + ", FieldName=" 626 + this.getFieldName(); 627 } 628 629 630 632 633 640 private void writeObject(ObjectOutputStream out) throws IOException { 641 if (dfos.isInMemory()) { 643 cachedContent = get(); 644 } else { 645 cachedContent = null; 646 dfosFile = dfos.getFile(); 647 } 648 649 out.defaultWriteObject(); 651 } 652 653 661 private void readObject(ObjectInputStream in) 662 throws IOException , ClassNotFoundException { 663 in.defaultReadObject(); 665 666 OutputStream output = getOutputStream(); 667 if (cachedContent != null) { 668 output.write(cachedContent); 669 } else { 670 FileInputStream input = new FileInputStream (dfosFile); 671 672 IOUtils.copy(input, output); 673 dfosFile.delete(); 674 dfosFile = null; 675 } 676 output.close(); 677 678 cachedContent = null; 679 } 680 681 } 682 | Popular Tags |