1 17 18 19 package org.apache.tomcat.util.http.fileupload; 20 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.UnsupportedEncodingException ; 32 33 34 56 public class DefaultFileItem 57 implements FileItem 58 { 59 60 62 63 66 private static int counter = 0; 67 68 69 72 private String fieldName; 73 74 75 79 private String contentType; 80 81 82 85 private boolean isFormField; 86 87 88 91 private String fileName; 92 93 94 97 private int sizeThreshold; 98 99 100 103 private File repository; 104 105 106 109 private byte[] cachedContent; 110 111 112 115 private DeferredFileOutputStream dfos; 116 117 118 120 121 138 DefaultFileItem(String fieldName, String contentType, boolean isFormField, 139 String fileName, int sizeThreshold, File repository) 140 { 141 this.fieldName = fieldName; 142 this.contentType = contentType; 143 this.isFormField = isFormField; 144 this.fileName = fileName; 145 this.sizeThreshold = sizeThreshold; 146 this.repository = repository; 147 } 148 149 150 152 153 162 public InputStream getInputStream() 163 throws IOException 164 { 165 if (!dfos.isInMemory()) 166 { 167 return new FileInputStream (dfos.getFile()); 168 } 169 170 if (cachedContent == null) 171 { 172 cachedContent = dfos.getData(); 173 } 174 return new ByteArrayInputStream (cachedContent); 175 } 176 177 178 185 public String getContentType() 186 { 187 return contentType; 188 } 189 190 191 196 public String getName() 197 { 198 return fileName; 199 } 200 201 202 204 205 212 public boolean isInMemory() 213 { 214 return (dfos.isInMemory()); 215 } 216 217 218 223 public long getSize() 224 { 225 if (cachedContent != null) 226 { 227 return cachedContent.length; 228 } 229 else if (dfos.isInMemory()) 230 { 231 return dfos.getData().length; 232 } 233 else 234 { 235 return dfos.getFile().length(); 236 } 237 } 238 239 240 247 public byte[] get() 248 { 249 if (dfos.isInMemory()) 250 { 251 if (cachedContent == null) 252 { 253 cachedContent = dfos.getData(); 254 } 255 return cachedContent; 256 } 257 258 byte[] fileData = new byte[(int) getSize()]; 259 FileInputStream fis = null; 260 261 try 262 { 263 fis = new FileInputStream (dfos.getFile()); 264 fis.read(fileData); 265 } 266 catch (IOException e) 267 { 268 fileData = null; 269 } 270 finally 271 { 272 if (fis != null) 273 { 274 try 275 { 276 fis.close(); 277 } 278 catch (IOException e) 279 { 280 } 282 } 283 } 284 285 return fileData; 286 } 287 288 289 301 public String getString(String encoding) 302 throws UnsupportedEncodingException 303 { 304 return new String (get(), encoding); 305 } 306 307 308 315 public String getString() 316 { 317 return new String (get()); 318 } 319 320 321 341 public void write(File file) throws Exception 342 { 343 if (isInMemory()) 344 { 345 FileOutputStream fout = null; 346 try 347 { 348 fout = new FileOutputStream (file); 349 fout.write(get()); 350 } 351 finally 352 { 353 if (fout != null) 354 { 355 fout.close(); 356 } 357 } 358 } 359 else 360 { 361 File outputFile = getStoreLocation(); 362 if (outputFile != null) 363 { 364 369 if (!outputFile.renameTo(file)) 370 { 371 BufferedInputStream in = null; 372 BufferedOutputStream out = null; 373 try 374 { 375 in = new BufferedInputStream ( 376 new FileInputStream (outputFile)); 377 out = new BufferedOutputStream ( 378 new FileOutputStream (file)); 379 byte[] bytes = new byte[2048]; 380 int s = 0; 381 while ((s = in.read(bytes)) != -1) 382 { 383 out.write(bytes, 0, s); 384 } 385 } 386 finally 387 { 388 try 389 { 390 in.close(); 391 } 392 catch (IOException e) 393 { 394 } 396 try 397 { 398 out.close(); 399 } 400 catch (IOException e) 401 { 402 } 404 } 405 } 406 } 407 else 408 { 409 413 throw new FileUploadException( 414 "Cannot write uploaded file to disk!"); 415 } 416 } 417 } 418 419 420 427 public void delete() 428 { 429 cachedContent = null; 430 File outputFile = getStoreLocation(); 431 if (outputFile != null && outputFile.exists()) 432 { 433 outputFile.delete(); 434 } 435 } 436 437 438 447 public String getFieldName() 448 { 449 return fieldName; 450 } 451 452 453 461 public void setFieldName(String fieldName) 462 { 463 this.fieldName = fieldName; 464 } 465 466 467 477 public boolean isFormField() 478 { 479 return isFormField; 480 } 481 482 483 493 public void setFormField(boolean state) 494 { 495 isFormField = state; 496 } 497 498 499 508 public OutputStream getOutputStream() 509 throws IOException 510 { 511 if (dfos == null) 512 { 513 File outputFile = getTempFile(); 514 dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); 515 } 516 return dfos; 517 } 518 519 520 522 523 536 public File getStoreLocation() 537 { 538 return dfos.getFile(); 539 } 540 541 542 544 545 548 protected void finalize() 549 { 550 File outputFile = dfos.getFile(); 551 552 if (outputFile != null && outputFile.exists()) 553 { 554 outputFile.delete(); 555 } 556 } 557 558 559 565 protected File getTempFile() 566 { 567 File tempDir = repository; 568 if (tempDir == null) 569 { 570 tempDir = new File (System.getProperty("java.io.tmpdir")); 571 } 572 573 String fileName = "upload_" + getUniqueId() + ".tmp"; 574 575 File f = new File (tempDir, fileName); 576 f.deleteOnExit(); 577 return f; 578 } 579 580 581 583 584 590 private static String getUniqueId() 591 { 592 int current; 593 synchronized (DefaultFileItem.class) 594 { 595 current = counter++; 596 } 597 String id = Integer.toString(current); 598 599 if (current < 100000000) 602 { 603 id = ("00000000" + id).substring(id.length()); 604 } 605 return id; 606 } 607 608 } 609 | Popular Tags |