1 61 62 63 package org.apache.commons.fileupload; 64 65 66 import java.io.BufferedInputStream ; 67 import java.io.BufferedOutputStream ; 68 import java.io.ByteArrayInputStream ; 69 import java.io.File ; 70 import java.io.FileInputStream ; 71 import java.io.FileOutputStream ; 72 import java.io.IOException ; 73 import java.io.InputStream ; 74 import java.io.OutputStream ; 75 import java.io.UnsupportedEncodingException ; 76 77 78 100 public class DefaultFileItem 101 implements FileItem 102 { 103 104 106 107 110 private static int counter = 0; 111 112 113 116 private String fieldName; 117 118 119 123 private String contentType; 124 125 126 129 private boolean isFormField; 130 131 132 135 private String fileName; 136 137 138 141 private int sizeThreshold; 142 143 144 147 private File repository; 148 149 150 153 private byte[] cachedContent; 154 155 156 159 private DeferredFileOutputStream dfos; 160 161 162 164 165 182 DefaultFileItem(String fieldName, String contentType, boolean isFormField, 183 String fileName, int sizeThreshold, File repository) 184 { 185 this.fieldName = fieldName; 186 this.contentType = contentType; 187 this.isFormField = isFormField; 188 this.fileName = fileName; 189 this.sizeThreshold = sizeThreshold; 190 this.repository = repository; 191 } 192 193 194 196 197 206 public InputStream getInputStream() 207 throws IOException 208 { 209 if (!dfos.isInMemory()) 210 { 211 return new FileInputStream (dfos.getFile()); 212 } 213 214 if (cachedContent == null) 215 { 216 cachedContent = dfos.getData(); 217 } 218 return new ByteArrayInputStream (cachedContent); 219 } 220 221 222 229 public String getContentType() 230 { 231 return contentType; 232 } 233 234 235 240 public String getName() 241 { 242 return fileName; 243 } 244 245 246 248 249 256 public boolean isInMemory() 257 { 258 return (dfos.isInMemory()); 259 } 260 261 262 267 public long getSize() 268 { 269 if (cachedContent != null) 270 { 271 return cachedContent.length; 272 } 273 else if (dfos.isInMemory()) 274 { 275 return dfos.getData().length; 276 } 277 else 278 { 279 return dfos.getFile().length(); 280 } 281 } 282 283 284 291 public byte[] get() 292 { 293 if (dfos.isInMemory()) 294 { 295 if (cachedContent == null) 296 { 297 cachedContent = dfos.getData(); 298 } 299 return cachedContent; 300 } 301 302 byte[] fileData = new byte[(int) getSize()]; 303 FileInputStream fis = null; 304 305 try 306 { 307 fis = new FileInputStream (dfos.getFile()); 308 fis.read(fileData); 309 } 310 catch (IOException e) 311 { 312 fileData = null; 313 } 314 finally 315 { 316 if (fis != null) 317 { 318 try 319 { 320 fis.close(); 321 } 322 catch (IOException e) 323 { 324 } 326 } 327 } 328 329 return fileData; 330 } 331 332 333 345 public String getString(String encoding) 346 throws UnsupportedEncodingException 347 { 348 return new String (get(), encoding); 349 } 350 351 352 359 public String getString() 360 { 361 return new String (get()); 362 } 363 364 365 385 public void write(File file) throws Exception 386 { 387 if (isInMemory()) 388 { 389 FileOutputStream fout = null; 390 try 391 { 392 fout = new FileOutputStream (file); 393 fout.write(get()); 394 } 395 finally 396 { 397 if (fout != null) 398 { 399 fout.close(); 400 } 401 } 402 } 403 else 404 { 405 File outputFile = getStoreLocation(); 406 if (outputFile != null) 407 { 408 413 if (!outputFile.renameTo(file)) 414 { 415 BufferedInputStream in = null; 416 BufferedOutputStream out = null; 417 try 418 { 419 in = new BufferedInputStream ( 420 new FileInputStream (outputFile)); 421 out = new BufferedOutputStream ( 422 new FileOutputStream (file)); 423 byte[] bytes = new byte[2048]; 424 int s = 0; 425 while ((s = in.read(bytes)) != -1) 426 { 427 out.write(bytes, 0, s); 428 } 429 } 430 finally 431 { 432 try 433 { 434 in.close(); 435 } 436 catch (IOException e) 437 { 438 } 440 try 441 { 442 out.close(); 443 } 444 catch (IOException e) 445 { 446 } 448 } 449 } 450 } 451 else 452 { 453 457 throw new FileUploadException( 458 "Cannot write uploaded file to disk!"); 459 } 460 } 461 } 462 463 464 471 public void delete() 472 { 473 cachedContent = null; 474 File outputFile = getStoreLocation(); 475 if (outputFile != null && outputFile.exists()) 476 { 477 outputFile.delete(); 478 } 479 } 480 481 482 491 public String getFieldName() 492 { 493 return fieldName; 494 } 495 496 497 505 public void setFieldName(String fieldName) 506 { 507 this.fieldName = fieldName; 508 } 509 510 511 521 public boolean isFormField() 522 { 523 return isFormField; 524 } 525 526 527 537 public void setFormField(boolean state) 538 { 539 isFormField = state; 540 } 541 542 543 552 public OutputStream getOutputStream() 553 throws IOException 554 { 555 if (dfos == null) 556 { 557 File outputFile = getTempFile(); 558 dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); 559 } 560 return dfos; 561 } 562 563 564 566 567 580 public File getStoreLocation() 581 { 582 return dfos.getFile(); 583 } 584 585 586 588 589 592 protected void finalize() 593 { 594 File outputFile = dfos.getFile(); 595 596 if (outputFile != null && outputFile.exists()) 597 { 598 outputFile.delete(); 599 } 600 } 601 602 603 609 protected File getTempFile() 610 { 611 File tempDir = repository; 612 if (tempDir == null) 613 { 614 tempDir = new File (System.getProperty("java.io.tmpdir")); 615 } 616 617 String fileName = "upload_" + getUniqueId() + ".tmp"; 618 619 File f = new File (tempDir, fileName); 620 f.deleteOnExit(); 621 return f; 622 } 623 624 625 627 628 634 private static String getUniqueId() 635 { 636 int current; 637 synchronized (DefaultFileItem.class) 638 { 639 current = counter++; 640 } 641 String id = Integer.toString(current); 642 643 if (current < 100000000) 646 { 647 id = ("00000000" + id).substring(id.length()); 648 } 649 return id; 650 } 651 652 } 653 | Popular Tags |