org.apache.commons.fileupload |
Overview of the org.apache.commons.fileupload component
A component for handling HTML file uploads as specified by
RFC 1867.
This component provides support for uploads within both servlets (JSR 53)
and portlets (JSR 168).
While this package provides the generic functionality for file uploads,
these classes are not typically used directly. Instead, normal usage
involves one of the provided extensions of
{@link org.apache.commons.fileupload.FileUpload FileUpload} such as
{@link org.apache.commons.fileupload.servlet.ServletFileUpload ServletFileUpload}
or
{@link org.apache.commons.fileupload.portlet.PortletFileUpload PortletFileUpload},
together with a factory for
{@link org.apache.commons.fileupload.FileItem FileItem} instances,
such as
{@link org.apache.commons.fileupload.disk.DiskFileItemFactory DiskFileItemFactory}.
The following is a brief example of typical usage in a servlet, storing
the uploaded files on disk.
public void doPost(HttpServletRequest req, HttpServletResponse res) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File("/tmp"));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000);
List fileItems = upload.parseRequest(req);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
String comment = ((FileItem)i.next()).getString();
FileItem fi = (FileItem)i.next();
// filename on the client
String fileName = fi.getName();
// save comment and filename to database
...
// write the file
fi.write(new File("/www/uploads/", fileName));
}
In the example above, the first file is loaded into memory as a
String . Before calling the getString method,
the data may have been in memory or on disk depending on its size. The
second file we assume it will be large and therefore never explicitly
load it into memory, though if it is less than 4096 bytes it will be
in memory before it is written to its final location. When writing to
the final location, if the data is larger than the threshold, an attempt
is made to rename the temporary file to the given location. If it cannot
be renamed, it is streamed to the new location.
Please see the FileUpload
User Guide
for further details and examples of how to use this package.
|
Java Source File Name | Type | Comment |
DefaultFileItem.java | Class | The default implementation of the
org.apache.commons.fileupload.FileItem FileItem interface.
After retrieving an instance of this class from a
org.apache.commons.fileupload.DiskFileUpload DiskFileUpload instance (see
org.apache.commons.fileupload.DiskFileUpload.parseRequest(javax.servlet.http.HttpServletRequest) ), you may
either request all contents of file at once using
DefaultFileItem.get() or
request an
java.io.InputStream InputStream with
DefaultFileItem.getInputStream() and process the file without attempting to load
it into memory, which may come handy with large files.
author: Rafal Krzewski author: Sean Legassick author: Jason van Zyl author: John McNally author: Martin Cooper author: Sean C. |
DefaultFileItemFactory.java | Class | The default
org.apache.commons.fileupload.FileItemFactory implementation. |
DefaultFileItemTest.java | Class | Unit tests for
org.apache.commons.fileupload.DefaultFileItem . |
DiskFileItemSerializeTest.java | Class | Serialization Unit tests for
org.apache.commons.fileupload.disk.DiskFileItem . |
DiskFileUpload.java | Class | High level API for processing file uploads.
This class handles multiple files per single HTML widget, sent using
multipart/mixed encoding type, as specified by
RFC 1867. |
FileItem.java | Interface | This class represents a file or form item that was received within a
multipart/form-data POST request.
After retrieving an instance of this class from a
org.apache.commons.fileupload.FileUpload FileUpload instance (see
org.apache.commons.fileupload.FileUpload.parseRequest(javax.servlet.http.HttpServletRequest) ), you may
either request all contents of the file at once using
FileItem.get() or
request an
java.io.InputStream InputStream with
FileItem.getInputStream() and process the file without attempting to load
it into memory, which may come handy with large files.
While this interface does not extend
javax.activation.DataSource per se (to avoid a seldom used
dependency), several of the defined methods are specifically defined with
the same signatures as methods in that interface. |
FileItemFactory.java | Interface | A factory interface for creating
FileItem instances. |
FileItemIterator.java | Interface | An iterator, as returned by
FileUploadBase.getItemIterator(RequestContext) . |
FileItemStream.java | Interface | This interface provides access to a file or form item that was
received within a multipart/form-data POST request. |
FileUpload.java | Class | High level API for processing file uploads.
This class handles multiple files per single HTML widget, sent using
multipart/mixed encoding type, as specified by
RFC 1867. |
FileUploadBase.java | Class | High level API for processing file uploads.
This class handles multiple files per single HTML widget, sent using
multipart/mixed encoding type, as specified by
RFC 1867. |
FileUploadException.java | Class | Exception for errors encountered while processing the request. |
FileUploadTestCase.java | Class | Base class for deriving test cases. |
HttpServletRequestFactory.java | Class | |
MockHttpServletRequest.java | Class | |
MockPortletRequest.java | Class | |
MockPortletSession.java | Class | |
MultipartStream.java | Class | Low level API for processing file uploads.
This class can be used to process data streams conforming to MIME
'multipart' format as defined in
RFC 1867. |
MultipartStreamTest.java | Class | Unit tests
org.apache.commons.fileupload.MultipartStream .
author: Sean C. |
ParameterParser.java | Class | A simple parser intended to parse sequences of name/value pairs. |
ParameterParserTest.java | Class | Unit tests for
ParameterParser . |
ProgressListener.java | Interface | The
ProgressListener may be used to display a progress bar
or do stuff like that. |
ProgressListenerTest.java | Class | Tests the progress listener. |
RequestContext.java | Interface | Abstracts access to the request information needed for file uploads. |
ServletFileUploadTest.java | Class | Unit tests
org.apache.commons.fileupload.DiskFileUpload .
author: John McNally author: Sean C. |
SizesTest.java | Class | Unit test for items with varying sizes. |
StreamingTest.java | Class | Unit test for items with varying sizes. |
TestAll.java | Class | author: Sean C. |