Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.jcorporate.expresso.core.misc.upload
Class FileItem  view FileItem download FileItem.java

java.lang.Object
  extended bycom.jcorporate.expresso.core.misc.upload.FileItem
All Implemented Interfaces:
javax.activation.DataSource, org.apache.struts.upload.FormFile

public class FileItem
extends java.lang.Object
implements org.apache.struts.upload.FormFile, javax.activation.DataSource

This class represents a file that was received by Turbine using multipart/form-data POST request.

After retrieving an instance of this class from the ParameterParser (see ParameterParser.getFileItem(String) and ParameterParser.getFileItems(String)) you can use it to acces the data that was sent by the browser. You may either request all contents of file at once using get() 55 or request an InputStream with getStream() 55 and process the file without attempting to load it into memory, which may come handy with large files.

Version:
$Id: FileItem.java,v 1.8 2004/11/17 20:48:13 lhamel Exp $

Field Summary
protected  java.io.ByteArrayOutputStream byteStream
          Temporary storage for in-memory files.
protected  byte[] content
          Cached contents of the file.
protected  java.lang.String contentType
          The content type passed by the browser or null if not defined.
static int DEFAULT_UPLOAD_SIZE_THRESHOLD
          The maximal size of request that will have it's elements stored in memory.
protected  java.lang.String fileName
          The original filename in the user's filesystem.
protected  java.io.File storeLocation
          Temporary storage location.
 
Constructor Summary
protected FileItem(java.lang.String fileName, java.lang.String contentType)
          Constructs a new FileItem.
 
Method Summary
 void destroy()
          Destroy all content for this form file.
 byte[] get()
          Returns the contents of the file as an array of bytes.
 java.lang.String getContentType()
          Returns the content type passed by the browser or null if not defined.
 byte[] getFileData()
          Get the data in byte array for for this file.
 java.lang.String getFileName()
          Returns the original filename in the user's filesystem.
 int getFileSize()
          Get the size of this file
 java.io.InputStream getInputStream()
          Get an InputStream that represents this file.
 java.lang.String getName()
          Returns the name of the FileItem
 java.io.OutputStream getOutputStream()
          Returns an OutputStream that can be used for storing the contensts of the file.
 long getSize()
          Returns the size of the file.
 java.io.File getStoreLocation()
          Returns the java.io.File objects for the FileItems's data temporary location on the disk.
 java.io.InputStream getStream()
          Returns an InputStream that can be used to retrieve the contents of the file.
 java.lang.String getString()
          Returns the contents of the file as a String, using default encoding.
 java.lang.String getString(java.lang.String encoding)
          Returns the contents of the file as a String, using specified encoding.
 boolean inMemory()
          Provides a hint if the file contents will be read from memory.
static FileItem newInstance(java.lang.String path, java.lang.String name, java.lang.String contentType, int requestSize, boolean storeAsFile)
          Instantiates a FileItem.
 void setContentType(java.lang.String contentType)
          Set the content type for this file
 void setFileName(java.lang.String newFileName)
          Set the filename of this file
 void setFileSize(int filesize)
          Set the file size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_UPLOAD_SIZE_THRESHOLD

public static final int DEFAULT_UPLOAD_SIZE_THRESHOLD
The maximal size of request that will have it's elements stored in memory.

See Also:
Constant Field Values

fileName

protected java.lang.String fileName
The original filename in the user's filesystem.


contentType

protected java.lang.String contentType
The content type passed by the browser or null if not defined.


content

protected byte[] content
Cached contents of the file.


storeLocation

protected java.io.File storeLocation
Temporary storage location.


byteStream

protected java.io.ByteArrayOutputStream byteStream
Temporary storage for in-memory files.

Constructor Detail

FileItem

protected FileItem(java.lang.String fileName,
                   java.lang.String contentType)
Constructs a new FileItem.

Use #newInstance(String,String,String,int) to instantiate FileItems.

Method Detail

getFileName

public java.lang.String getFileName()
Returns the original filename in the user's filesystem.

Specified by:
getFileName in interface org.apache.struts.upload.FormFile

getContentType

public java.lang.String getContentType()
Returns the content type passed by the browser or null if not defined.

Specified by:
getContentType in interface org.apache.struts.upload.FormFile

inMemory

public boolean inMemory()
Provides a hint if the file contents will be read from memory.


getSize

public long getSize()
Returns the size of the file.


get

public byte[] get()
Returns the contents of the file as an array of bytes. If the contents of the file were not yet cached int the memory, they will be loaded from the disk storage and chached.


getString

public java.lang.String getString()
Returns the contents of the file as a String, using default encoding. This method uses get() 55 to retrieve the contents of the file.


getString

public java.lang.String getString(java.lang.String encoding)
                           throws java.io.UnsupportedEncodingException
Returns the contents of the file as a String, using specified encoding. This method uses get() 55 to retireve the contents of the file.


getStream

public java.io.InputStream getStream()
                              throws java.lang.Exception
Returns an InputStream that can be used to retrieve the contents of the file.


getStoreLocation

public java.io.File getStoreLocation()
Returns the java.io.File objects for the FileItems's data temporary location on the disk. Note that for FileItems that have their data stored in memory this method will return null. When handling large files, you can use File.renameTo(File)>File.renameTo(File) 55 to move the file to new location without copying the data, if the source and destination locations reside within the same logical volume.


getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Returns an OutputStream that can be used for storing the contensts of the file.

Specified by:
getOutputStream in interface javax.activation.DataSource

newInstance

public static FileItem newInstance(java.lang.String path,
                                   java.lang.String name,
                                   java.lang.String contentType,
                                   int requestSize,
                                   boolean storeAsFile)
Instantiates a FileItem. It uses requestSize to decide what temporary storage approach the new item should take. The largest request that will have its items cached in memory can be configured in TurbineResources.properties in the entry named file.upload.size.threshold


setContentType

public void setContentType(java.lang.String contentType)
Set the content type for this file

Specified by:
setContentType in interface org.apache.struts.upload.FormFile

getFileSize

public int getFileSize()
Get the size of this file

Specified by:
getFileSize in interface org.apache.struts.upload.FormFile

setFileSize

public void setFileSize(int filesize)
Set the file size

Specified by:
setFileSize in interface org.apache.struts.upload.FormFile

setFileName

public void setFileName(java.lang.String newFileName)
Set the filename of this file

Specified by:
setFileName in interface org.apache.struts.upload.FormFile

getFileData

public byte[] getFileData()
                   throws java.io.FileNotFoundException,
                          java.io.IOException
Get the data in byte array for for this file. Note that this can be a very hazardous method, files can be large enough to cause OutOfMemoryErrors. Short of being deprecated, it's strongly recommended that you use getInputStream 55 to get the file data.

Specified by:
getFileData in interface org.apache.struts.upload.FormFile

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.FileNotFoundException,
                                          java.io.IOException
Get an InputStream that represents this file. This is the preferred method of getting file data.

Specified by:
getInputStream in interface org.apache.struts.upload.FormFile

destroy

public void destroy()
Destroy all content for this form file. Implementations should remove any temporary files or any temporary file data stored somewhere

Specified by:
destroy in interface org.apache.struts.upload.FormFile

getName

public java.lang.String getName()
Returns the name of the FileItem

Specified by:
getName in interface javax.activation.DataSource