org.apache.tomcat.util.http.fileupload |
Overview of the org.apache.commons.fileupload component
Component for handling html file uploads as given by rfc 1867
RFC 1867.
Normal usage of the package involves
{@link org.apache.commons.fileupload.DiskFileUpload DiskFileUpload}
parsing the HttpServletRequest and returning a list of
{@link org.apache.commons.fileupload.FileItem FileItem}'s.
These FileItem 's provide easy access to the data
given in the upload. There is also a low level api for
manipulating the upload data encapsulated in the
{@link org.apache.commons.fileupload.MultipartStream MultipartStream}
class.
Normal usage example:
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp");
List fileItems = fu.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("/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.
|
Java Source File Name | Type | Comment |
DefaultFileItem.java | Class | The default implementation of the
org.apache.tomcat.util.http.fileupload.FileItem FileItem interface.
After retrieving an instance of this class from a
org.apache.tomcat.util.http.fileupload.DiskFileUpload DiskFileUpload instance (see
org.apache.tomcat.util.http.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.tomcat.util.http.fileupload.FileItemFactory implementation. |
DeferredFileOutputStream.java | Class | An output stream which will retain data in memory until a specified
threshold is reached, and only then commit it to disk. |
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.tomcat.util.http.fileupload.FileUpload FileUpload instance (see
org.apache.tomcat.util.http.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. |
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. |
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. |
ThresholdingOutputStream.java | Class | An output stream which triggers an event when a specified number of bytes of
data have been written to it. |