DefaultFileItem.java

00001 /*
00002  * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItem.java,v 1.21 2003/06/24 05:45:15 martinc Exp $
00003  * $Revision: 1.21 $
00004  * $Date: 2003/06/24 05:45:15 $
00005  *
00006  * ====================================================================
00007  *
00008  * The Apache Software License, Version 1.1
00009  *
00010  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
00011  * reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions
00015  * are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright
00021  *    notice, this list of conditions and the following disclaimer in
00022  *    the documentation and/or other materials provided with the
00023  *    distribution.
00024  *
00025  * 3. The end-user documentation included with the redistribution, if
00026  *    any, must include the following acknowlegement:
00027  *       "This product includes software developed by the
00028  *        Apache Software Foundation (http://www.apache.org/)."
00029  *    Alternately, this acknowlegement may appear in the software itself,
00030  *    if and wherever such third-party acknowlegements normally appear.
00031  *
00032  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
00033  *    Foundation" must not be used to endorse or promote products derived
00034  *    from this software without prior written permission. For written
00035  *    permission, please contact apache@apache.org.
00036  *
00037  * 5. Products derived from this software may not be called "Apache"
00038  *    nor may "Apache" appear in their names without prior written
00039  *    permission of the Apache Group.
00040  *
00041  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00042  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00043  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00044  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00045  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00046  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00047  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00048  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00049  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00050  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00051  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00052  * SUCH DAMAGE.
00053  * ====================================================================
00054  *
00055  * This software consists of voluntary contributions made by many
00056  * individuals on behalf of the Apache Software Foundation.  For more
00057  * information on the Apache Software Foundation, please see
00058  * <http://www.apache.org/>.
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     // ----------------------------------------------------------- Data members
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     // ----------------------------------------------------------- Constructors
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     // ------------------------------- Methods from javax.activation.DataSource
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     // ------------------------------------------------------- FileItem methods
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                     // ignore
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                  * The uploaded file is being stored on disk
00410                  * in a temporary location so move it to the
00411                  * desired file.
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                             // ignore
00439                         }
00440                         try
00441                         {
00442                             out.close();
00443                         }
00444                         catch (IOException e)
00445                         {
00446                             // ignore
00447                         }
00448                     }
00449                 }
00450             }
00451             else
00452             {
00453                 /*
00454                  * For whatever reason we cannot write the
00455                  * file to disk.
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     // --------------------------------------------------------- Public methods
00565 
00566 
00580     public File getStoreLocation()
00581     {
00582         return dfos.getFile();
00583     }
00584 
00585 
00586     // ------------------------------------------------------ Protected methods
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     // -------------------------------------------------------- Private methods
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         // If you manage to get more than 100 million of ids, you'll
00644         // start getting ids longer than 8 characters.
00645         if (current < 100000000)
00646         {
00647             id = ("00000000" + id).substring(id.length());
00648         }
00649         return id;
00650     }
00651 
00652 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4