00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 package org.apache.commons.fileupload;
00064
00065
00066 import java.io.BufferedInputStream;
00067 import java.io.BufferedOutputStream;
00068 import java.io.ByteArrayInputStream;
00069 import java.io.File;
00070 import java.io.FileInputStream;
00071 import java.io.FileOutputStream;
00072 import java.io.IOException;
00073 import java.io.InputStream;
00074 import java.io.OutputStream;
00075 import java.io.UnsupportedEncodingException;
00076
00077
00100 public class DefaultFileItem
00101 implements FileItem
00102 {
00103
00104
00105
00106
00110 private static int counter = 0;
00111
00112
00116 private String fieldName;
00117
00118
00123 private String contentType;
00124
00125
00129 private boolean isFormField;
00130
00131
00135 private String fileName;
00136
00137
00141 private int sizeThreshold;
00142
00143
00147 private File repository;
00148
00149
00153 private byte[] cachedContent;
00154
00155
00159 private DeferredFileOutputStream dfos;
00160
00161
00162
00163
00164
00182 DefaultFileItem(String fieldName, String contentType, boolean isFormField,
00183 String fileName, int sizeThreshold, File repository)
00184 {
00185 this.fieldName = fieldName;
00186 this.contentType = contentType;
00187 this.isFormField = isFormField;
00188 this.fileName = fileName;
00189 this.sizeThreshold = sizeThreshold;
00190 this.repository = repository;
00191 }
00192
00193
00194
00195
00196
00206 public InputStream getInputStream()
00207 throws IOException
00208 {
00209 if (!dfos.isInMemory())
00210 {
00211 return new FileInputStream(dfos.getFile());
00212 }
00213
00214 if (cachedContent == null)
00215 {
00216 cachedContent = dfos.getData();
00217 }
00218 return new ByteArrayInputStream(cachedContent);
00219 }
00220
00221
00229 public String getContentType()
00230 {
00231 return contentType;
00232 }
00233
00234
00240 public String getName()
00241 {
00242 return fileName;
00243 }
00244
00245
00246
00247
00248
00256 public boolean isInMemory()
00257 {
00258 return (dfos.isInMemory());
00259 }
00260
00261
00267 public long getSize()
00268 {
00269 if (cachedContent != null)
00270 {
00271 return cachedContent.length;
00272 }
00273 else if (dfos.isInMemory())
00274 {
00275 return dfos.getData().length;
00276 }
00277 else
00278 {
00279 return dfos.getFile().length();
00280 }
00281 }
00282
00283
00291 public byte[] get()
00292 {
00293 if (dfos.isInMemory())
00294 {
00295 if (cachedContent == null)
00296 {
00297 cachedContent = dfos.getData();
00298 }
00299 return cachedContent;
00300 }
00301
00302 byte[] fileData = new byte[(int) getSize()];
00303 FileInputStream fis = null;
00304
00305 try
00306 {
00307 fis = new FileInputStream(dfos.getFile());
00308 fis.read(fileData);
00309 }
00310 catch (IOException e)
00311 {
00312 fileData = null;
00313 }
00314 finally
00315 {
00316 if (fis != null)
00317 {
00318 try
00319 {
00320 fis.close();
00321 }
00322 catch (IOException e)
00323 {
00324
00325 }
00326 }
00327 }
00328
00329 return fileData;
00330 }
00331
00332
00345 public String getString(String encoding)
00346 throws UnsupportedEncodingException
00347 {
00348 return new String(get(), encoding);
00349 }
00350
00351
00359 public String getString()
00360 {
00361 return new String(get());
00362 }
00363
00364
00385 public void write(File file) throws Exception
00386 {
00387 if (isInMemory())
00388 {
00389 FileOutputStream fout = null;
00390 try
00391 {
00392 fout = new FileOutputStream(file);
00393 fout.write(get());
00394 }
00395 finally
00396 {
00397 if (fout != null)
00398 {
00399 fout.close();
00400 }
00401 }
00402 }
00403 else
00404 {
00405 File outputFile = getStoreLocation();
00406 if (outputFile != null)
00407 {
00408
00409
00410
00411
00412
00413 if (!outputFile.renameTo(file))
00414 {
00415 BufferedInputStream in = null;
00416 BufferedOutputStream out = null;
00417 try
00418 {
00419 in = new BufferedInputStream(
00420 new FileInputStream(outputFile));
00421 out = new BufferedOutputStream(
00422 new FileOutputStream(file));
00423 byte[] bytes = new byte[2048];
00424 int s = 0;
00425 while ((s = in.read(bytes)) != -1)
00426 {
00427 out.write(bytes, 0, s);
00428 }
00429 }
00430 finally
00431 {
00432 try
00433 {
00434 in.close();
00435 }
00436 catch (IOException e)
00437 {
00438
00439 }
00440 try
00441 {
00442 out.close();
00443 }
00444 catch (IOException e)
00445 {
00446
00447 }
00448 }
00449 }
00450 }
00451 else
00452 {
00453
00454
00455
00456
00457 throw new FileUploadException(
00458 "Cannot write uploaded file to disk!");
00459 }
00460 }
00461 }
00462
00463
00471 public void delete()
00472 {
00473 cachedContent = null;
00474 File outputFile = getStoreLocation();
00475 if (outputFile != null && outputFile.exists())
00476 {
00477 outputFile.delete();
00478 }
00479 }
00480
00481
00491 public String getFieldName()
00492 {
00493 return fieldName;
00494 }
00495
00496
00505 public void setFieldName(String fieldName)
00506 {
00507 this.fieldName = fieldName;
00508 }
00509
00510
00521 public boolean isFormField()
00522 {
00523 return isFormField;
00524 }
00525
00526
00537 public void setFormField(boolean state)
00538 {
00539 isFormField = state;
00540 }
00541
00542
00552 public OutputStream getOutputStream()
00553 throws IOException
00554 {
00555 if (dfos == null)
00556 {
00557 File outputFile = getTempFile();
00558 dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
00559 }
00560 return dfos;
00561 }
00562
00563
00564
00565
00566
00580 public File getStoreLocation()
00581 {
00582 return dfos.getFile();
00583 }
00584
00585
00586
00587
00588
00592 protected void finalize()
00593 {
00594 File outputFile = dfos.getFile();
00595
00596 if (outputFile != null && outputFile.exists())
00597 {
00598 outputFile.delete();
00599 }
00600 }
00601
00602
00609 protected File getTempFile()
00610 {
00611 File tempDir = repository;
00612 if (tempDir == null)
00613 {
00614 tempDir = new File(System.getProperty("java.io.tmpdir"));
00615 }
00616
00617 String fileName = "upload_" + getUniqueId() + ".tmp";
00618
00619 File f = new File(tempDir, fileName);
00620 f.deleteOnExit();
00621 return f;
00622 }
00623
00624
00625
00626
00627
00634 private static String getUniqueId()
00635 {
00636 int current;
00637 synchronized (DefaultFileItem.class)
00638 {
00639 current = counter++;
00640 }
00641 String id = Integer.toString(current);
00642
00643
00644
00645 if (current < 100000000)
00646 {
00647 id = ("00000000" + id).substring(id.length());
00648 }
00649 return id;
00650 }
00651
00652 }