org.apache.turbine.util.upload
Class FileItem

java.lang.Object
  |
  +--org.apache.turbine.util.upload.FileItem
All Implemented Interfaces:
javax.activation.DataSource

public class FileItem
extends java.lang.Object
implements 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() or request an InputStream with getStream() and process the file without attempting to load it into memory, which may come handy with large files. Implements the javax.activation.DataSource interface (which allows for example the adding of a FileItem as an attachment to a multipart email).

Version:
$Id: FileItem.java,v 1.3 2001/08/23 19:46:37 dlr Exp $
Author:
Rafal Krzewski, Sean Legassick, Jason van Zyl

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
protected  void finalize()
          Removes the file contents from the temporary storage.
 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.
 java.lang.String getFileName()
          Returns the original filename in the user's filesystem.
 java.io.InputStream getInputStream()
          Returns an InputStream that can be used to retrieve the contents of the file.
 java.lang.String getName()
          Returns the original filename in the user's filesystem.
 java.io.OutputStream getOutputStream()
          Returns an OutputStream that can be used for storing the contents of the file.
 long getSize()
          Returns the size of the file.
 java.io.File getStoreLocation()
          Returns the 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)
          Instantiates a FileItem.
 void write(java.lang.String file)
          A convenience method to write an uploaded file to disk.
 
Methods inherited from class java.lang.Object
, clone, equals, getClass, hashCode, notify, notifyAll, registerNatives, 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.

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.

Parameters:
fileName - The original filename in the user's filesystem.
contentType - The content type passed by the browser or null if not defined.
Method Detail

getName

public java.lang.String getName()
Returns the original filename in the user's filesystem. (implements DataSource method)
Specified by:
getName in interface javax.activation.DataSource
Returns:
The original filename in the user's filesystem.

getFileName

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

getContentType

public java.lang.String getContentType()
Returns the content type passed by the browser or null if not defined. (implements DataSource method).
Specified by:
getContentType in interface javax.activation.DataSource
Returns:
The content type passed by the browser or null if not defined.

inMemory

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

getSize

public long getSize()
Returns the size of the file.
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.
Returns:
The contents of the file as an array of bytes.

getString

public java.lang.String getString()
Returns the contents of the file as a String, using default encoding. This method uses get() to retrieve the contents of the file.
Returns:
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() to retireve the contents of the file.
Parameters:
encoding - The encoding to use.
Returns:
The contents of the file.
Throws:
UnsupportedEncodingException. -  

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an InputStream that can be used to retrieve the contents of the file. (implements DataSource method)
Specified by:
getInputStream in interface javax.activation.DataSource
Returns:
An InputStream that can be used to retrieve the contents of the file.
Throws:
Exception, - a generic exception.

getStream

public java.io.InputStream getStream()
                              throws java.io.IOException
Returns an InputStream that can be used to retrieve the contents of the file.
Returns:
An InputStream that can be used to retrieve the contents of the file.
Throws:
Exception, - a generic exception.

getStoreLocation

public java.io.File getStoreLocation()
Returns the 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) to move the file to new location without copying the data, if the source and destination locations reside within the same logical volume.
Returns:
A File.

finalize

protected void finalize()
Removes the file contents from the temporary storage.
Overrides:
finalize in class java.lang.Object

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Returns an OutputStream that can be used for storing the contents of the file. (implements DataSource method)
Specified by:
getOutputStream in interface javax.activation.DataSource
Returns:
an OutputStream that can be used for storing the contensts of the file.
Throws:
IOException. -  

newInstance

public static FileItem newInstance(java.lang.String path,
                                   java.lang.String name,
                                   java.lang.String contentType,
                                   int requestSize)
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
Parameters:
path - A String.
name - The original filename in the user's filesystem.
contentType - The content type passed by the browser or null if not defined.
requestSize - The total size of the POST request this item belongs to.
Returns:
A FileItem.

write

public void write(java.lang.String file)
           throws java.lang.Exception
A convenience method to write an uploaded file to disk. The client code is not concerned whether or not the file is stored in memory, or on disk in a temporary location. They just want to write the uploaded file to disk.
Parameters:
String - full path to location where uploaded should be stored.


Copyright © 2000-2002 Apache Software Foundation. All Rights Reserved.