1 16 package net.jforum.util.legacy.commons.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.UnsupportedEncodingException ; 28 import java.util.Map ; 29 30 import net.jforum.util.legacy.commons.fileupload.FileItem; 31 import net.jforum.util.legacy.commons.fileupload.FileUploadException; 32 import net.jforum.util.legacy.commons.fileupload.ParameterParser; 33 34 import org.apache.commons.io.FileCleaner; 35 import org.apache.commons.io.output.DeferredFileOutputStream; 36 37 38 62 public class DiskFileItem 63 implements FileItem { 64 65 67 68 74 public static final String DEFAULT_CHARSET = "ISO-8859-1"; 75 76 77 80 private static final int WRITE_BUFFER_SIZE = 2048; 81 82 83 85 86 89 private static int counter = 0; 90 91 92 95 private String fieldName; 96 97 98 102 private String contentType; 103 104 105 108 private boolean isFormField; 109 110 111 114 private String fileName; 115 116 117 120 private int sizeThreshold; 121 122 123 126 private File repository; 127 128 129 132 private byte[] cachedContent; 133 134 135 138 private DeferredFileOutputStream dfos; 139 140 141 143 144 161 public DiskFileItem(String fieldName, String contentType, 162 boolean isFormField, String fileName, int sizeThreshold, 163 File repository) { 164 this.fieldName = fieldName; 165 this.contentType = contentType; 166 this.isFormField = isFormField; 167 this.fileName = fileName; 168 this.sizeThreshold = sizeThreshold; 169 this.repository = repository; 170 } 171 172 173 175 176 185 public InputStream getInputStream() 186 throws IOException { 187 if (!dfos.isInMemory()) { 188 return new FileInputStream (dfos.getFile()); 189 } 190 191 if (cachedContent == null) { 192 cachedContent = dfos.getData(); 193 } 194 return new ByteArrayInputStream (cachedContent); 195 } 196 197 198 205 public String getContentType() { 206 return contentType; 207 } 208 209 210 217 public String getCharSet() { 218 ParameterParser parser = new ParameterParser(); 219 parser.setLowerCaseNames(true); 220 Map params = parser.parse(getContentType(), ';'); 222 return (String ) params.get("charset"); 223 } 224 225 226 231 public String getName() { 232 return fileName; 233 } 234 235 236 238 239 246 public boolean isInMemory() { 247 return (dfos.isInMemory()); 248 } 249 250 251 256 public long getSize() { 257 if (cachedContent != null) { 258 return cachedContent.length; 259 } else if (dfos.isInMemory()) { 260 return dfos.getData().length; 261 } else { 262 return dfos.getFile().length(); 263 } 264 } 265 266 267 274 public byte[] get() { 275 if (dfos.isInMemory()) { 276 if (cachedContent == null) { 277 cachedContent = dfos.getData(); 278 } 279 return cachedContent; 280 } 281 282 byte[] fileData = new byte[(int) getSize()]; 283 FileInputStream fis = null; 284 285 try { 286 fis = new FileInputStream (dfos.getFile()); 287 fis.read(fileData); 288 } catch (IOException e) { 289 fileData = null; 290 } finally { 291 if (fis != null) { 292 try { 293 fis.close(); 294 } catch (IOException e) { 295 } 297 } 298 } 299 300 return fileData; 301 } 302 303 304 316 public String getString(final String charset) 317 throws UnsupportedEncodingException { 318 return new String (get(), charset); 319 } 320 321 322 331 public String getString() { 332 byte[] rawdata = get(); 333 String charset = getCharSet(); 334 if (charset == null) { 335 charset = DEFAULT_CHARSET; 336 } 337 try { 338 return new String (rawdata, charset); 339 } catch (UnsupportedEncodingException e) { 340 return new String (rawdata); 341 } 342 } 343 344 345 365 public void write(File file) throws Exception { 366 if (isInMemory()) { 367 FileOutputStream fout = null; 368 try { 369 fout = new FileOutputStream (file); 370 fout.write(get()); 371 } finally { 372 if (fout != null) { 373 fout.close(); 374 } 375 } 376 } else { 377 File outputFile = getStoreLocation(); 378 if (outputFile != null) { 379 384 if (!outputFile.renameTo(file)) { 385 BufferedInputStream in = null; 386 BufferedOutputStream out = null; 387 try { 388 in = new BufferedInputStream ( 389 new FileInputStream (outputFile)); 390 out = new BufferedOutputStream ( 391 new FileOutputStream (file)); 392 byte[] bytes = new byte[WRITE_BUFFER_SIZE]; 393 int s = 0; 394 while ((s = in.read(bytes)) != -1) { 395 out.write(bytes, 0, s); 396 } 397 } finally { 398 if (in != null) { 399 try { 400 in.close(); 401 } catch (IOException e) { 402 } 404 } 405 if (out != null) { 406 try { 407 out.close(); 408 } catch (IOException e) { 409 } 411 } 412 } 413 } 414 } else { 415 419 throw new FileUploadException( 420 "Cannot write uploaded file to disk!"); 421 } 422 } 423 } 424 425 426 433 public void delete() { 434 cachedContent = null; 435 File outputFile = getStoreLocation(); 436 if (outputFile != null && outputFile.exists()) { 437 outputFile.delete(); 438 } 439 } 440 441 442 451 public String getFieldName() { 452 return fieldName; 453 } 454 455 456 464 public void setFieldName(String fieldName) { 465 this.fieldName = fieldName; 466 } 467 468 469 479 public boolean isFormField() { 480 return isFormField; 481 } 482 483 484 494 public void setFormField(boolean state) { 495 isFormField = state; 496 } 497 498 499 508 public OutputStream getOutputStream() 509 throws IOException { 510 if (dfos == null) { 511 File outputFile = getTempFile(); 512 dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); 513 } 514 return dfos; 515 } 516 517 518 520 521 534 public File getStoreLocation() { 535 return dfos.getFile(); 536 } 537 538 539 541 542 545 protected void finalize() { 546 File outputFile = dfos.getFile(); 547 548 if (outputFile != null && outputFile.exists()) { 549 outputFile.delete(); 550 } 551 } 552 553 554 562 protected File getTempFile() { 563 File tempDir = repository; 564 if (tempDir == null) { 565 tempDir = new File (System.getProperty("java.io.tmpdir")); 566 } 567 568 String fileName = "upload_" + getUniqueId() + ".tmp"; 569 570 File f = new File (tempDir, fileName); 571 FileCleaner.track(f, this); 572 return f; 573 } 574 575 576 578 579 585 private static String getUniqueId() { 586 int current; 587 synchronized (DiskFileItem.class) { 588 current = counter++; 589 } 590 String id = Integer.toString(current); 591 592 if (current < 100000000) { 595 id = ("00000000" + id).substring(id.length()); 596 } 597 return id; 598 } 599 600 public String toString() { 601 return "name=" + this.getName() 602 + ", StoreLocation=" 603 + String.valueOf(this.getStoreLocation()) 604 + ", size=" 605 + this.getSize() 606 + "bytes, " 607 + "isFormField=" + isFormField() 608 + ", FieldName=" 609 + this.getFieldName(); 610 } 611 612 } 613 | Popular Tags |