com.oreilly.servlet
Class MultipartRequest

java.lang.Object
  extended by com.oreilly.servlet.MultipartRequest

public class MultipartRequest
extends java.lang.Object

A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. This class emulates the interface of HttpServletRequest, making it familiar to use. It uses a "push" model where any incoming files are read and saved directly to disk in the constructor. If you wish to have more flexibility, e.g. write the files to a database, use the "pull" model MultipartParser instead.

This class can receive arbitrarily large files (up to an artificial limit you can set), and fairly efficiently too. It cannot handle nested data (multipart content within multipart content). It can now with the latest release handle internationalized content (such as non Latin-1 filenames).

To avoid collisions and have fine control over file placement, there's a constructor variety that takes a pluggable FileRenamePolicy implementation. A particular policy can choose to rename or change the location of the file before it's written.

See the included upload.war for an example of how to use this class.

The full file upload specification is contained in experimental RFC 1867, available at http://www.ietf.org/rfc/rfc1867.txt.

Version:
1.11, 2002/11/01, combine query string params in param list
, 1.10, 2002/05/27, added access to the original file names
, 1.9, 2002/04/30, added support for file renaming, thanks to Changshin Lee
, 1.8, 2002/04/30, added support for internationalization, thanks to Changshin Lee
, 1.7, 2001/02/07, made fields protected to increase user flexibility
, 1.6, 2000/07/21, redid internals to use MultipartParser, thanks to Geoff Soutter
, 1.5, 2000/02/04, added auto MacBinary decoding for IE on Mac
, 1.4, 2000/01/05, added getParameterValues(), WebSphere 2.x getContentType() workaround, stopped writing empty "unknown" file
, 1.3, 1999/12/28, IE4 on Win98 lastIndexOf("boundary=") workaround
, 1.2, 1999/12/20, IE4 on Mac readNextPart() workaround
, 1.1, 1999/01/15, JSDK readLine() bug workaround
, 1.0, 1998/09/18
Author:
Jason Hunter, Geoff Soutter
See Also:
MultipartParser

Constructor Summary
MultipartRequest(HttpServletRequest request, java.lang.String saveDirectory)
          Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to 1 Megabyte.
MultipartRequest(HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize)
          Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length.
MultipartRequest(HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, FileRenamePolicy policy)
          Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length.
MultipartRequest(HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, java.lang.String encoding)
          Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length.
MultipartRequest(HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, java.lang.String encoding, FileRenamePolicy policy)
          Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length.
MultipartRequest(HttpServletRequest request, java.lang.String saveDirectory, java.lang.String encoding)
          Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length.
 
Method Summary
 java.lang.String getContentType(java.lang.String name)
          Returns the content type of the specified file (as supplied by the client browser), or null if the file was not included in the upload.
 java.io.File getFile(java.lang.String name)
          Returns a File object for the specified file saved on the server's filesystem, or null if the file was not included in the upload.
 java.util.Enumeration getFileNames()
          Returns the names of all the uploaded files as an Enumeration of Strings.
 java.lang.String getFilesystemName(java.lang.String name)
          Returns the filesystem name of the specified file, or null if the file was not included in the upload.
 java.lang.String getOriginalFileName(java.lang.String name)
          Returns the original filesystem name of the specified file (before any renaming policy was applied), or null if the file was not included in the upload.
 java.lang.String getParameter(java.lang.String name)
          Returns the value of the named parameter as a String, or null if the parameter was not sent or was sent without a value.
 java.util.Enumeration getParameterNames()
          Returns the names of all the parameters as an Enumeration of Strings.
 java.lang.String[] getParameterValues(java.lang.String name)
          Returns the values of the named parameter as a String array, or null if the parameter was not sent.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultipartRequest

public MultipartRequest(HttpServletRequest request,
                        java.lang.String saveDirectory)
                 throws java.io.IOException
Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to 1 Megabyte. If the content is too large, an IOException is thrown. This constructor actually parses the multipart/form-data and throws an IOException if there's any problem reading or parsing the request.

Parameters:
request - the servlet request.
saveDirectory - the directory in which to save any uploaded files.
Throws:
java.io.IOException - if the uploaded content is larger than 1 Megabyte or there's a problem reading or parsing the request.

MultipartRequest

public MultipartRequest(HttpServletRequest request,
                        java.lang.String saveDirectory,
                        int maxPostSize)
                 throws java.io.IOException
Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length. If the content is too large, an IOException is thrown. This constructor actually parses the multipart/form-data and throws an IOException if there's any problem reading or parsing the request.

Parameters:
request - the servlet request.
saveDirectory - the directory in which to save any uploaded files.
maxPostSize - the maximum size of the POST content.
Throws:
java.io.IOException - if the uploaded content is larger than maxPostSize or there's a problem reading or parsing the request.

MultipartRequest

public MultipartRequest(HttpServletRequest request,
                        java.lang.String saveDirectory,
                        java.lang.String encoding)
                 throws java.io.IOException
Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length. If the content is too large, an IOException is thrown. This constructor actually parses the multipart/form-data and throws an IOException if there's any problem reading or parsing the request.

Parameters:
request - the servlet request.
saveDirectory - the directory in which to save any uploaded files.
encoding - the encoding of the response, such as ISO-8859-1
Throws:
java.io.IOException - if the uploaded content is larger than 1 Megabyte or there's a problem reading or parsing the request.

MultipartRequest

public MultipartRequest(HttpServletRequest request,
                        java.lang.String saveDirectory,
                        int maxPostSize,
                        FileRenamePolicy policy)
                 throws java.io.IOException
Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length. If the content is too large, an IOException is thrown. This constructor actually parses the multipart/form-data and throws an IOException if there's any problem reading or parsing the request.

Parameters:
request - the servlet request.
saveDirectory - the directory in which to save any uploaded files.
maxPostSize - the maximum size of the POST content.
encoding - the encoding of the response, such as ISO-8859-1
Throws:
java.io.IOException - if the uploaded content is larger than maxPostSize or there's a problem reading or parsing the request.

MultipartRequest

public MultipartRequest(HttpServletRequest request,
                        java.lang.String saveDirectory,
                        int maxPostSize,
                        java.lang.String encoding)
                 throws java.io.IOException
Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length. If the content is too large, an IOException is thrown. This constructor actually parses the multipart/form-data and throws an IOException if there's any problem reading or parsing the request.

Parameters:
request - the servlet request.
saveDirectory - the directory in which to save any uploaded files.
maxPostSize - the maximum size of the POST content.
encoding - the encoding of the response, such as ISO-8859-1
Throws:
java.io.IOException - if the uploaded content is larger than maxPostSize or there's a problem reading or parsing the request.

MultipartRequest

public MultipartRequest(HttpServletRequest request,
                        java.lang.String saveDirectory,
                        int maxPostSize,
                        java.lang.String encoding,
                        FileRenamePolicy policy)
                 throws java.io.IOException
Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to the given directory, and limiting the upload size to the specified length. If the content is too large, an IOException is thrown. This constructor actually parses the multipart/form-data and throws an IOException if there's any problem reading or parsing the request. To avoid file collisions, this constructor takes an implementation of the FileRenamePolicy interface to allow a pluggable rename policy.

Parameters:
request - the servlet request.
saveDirectory - the directory in which to save any uploaded files.
maxPostSize - the maximum size of the POST content.
encoding - the encoding of the response, such as ISO-8859-1
policy - a pluggable file rename policy
Throws:
java.io.IOException - if the uploaded content is larger than maxPostSize or there's a problem reading or parsing the request.
Method Detail

getParameterNames

public java.util.Enumeration getParameterNames()
Returns the names of all the parameters as an Enumeration of Strings. It returns an empty Enumeration if there are no parameters.

Returns:
the names of all the parameters as an Enumeration of Strings.

getFileNames

public java.util.Enumeration getFileNames()
Returns the names of all the uploaded files as an Enumeration of Strings. It returns an empty Enumeration if there are no uploaded files. Each file name is the name specified by the form, not by the user.

Returns:
the names of all the uploaded files as an Enumeration of Strings.

getParameter

public java.lang.String getParameter(java.lang.String name)
Returns the value of the named parameter as a String, or null if the parameter was not sent or was sent without a value. The value is guaranteed to be in its normal, decoded form. If the parameter has multiple values, only the last one is returned (for backward compatibility). For parameters with multiple values, it's possible the last "value" may be null.

Parameters:
name - the parameter name.
Returns:
the parameter value.

getParameterValues

public java.lang.String[] getParameterValues(java.lang.String name)
Returns the values of the named parameter as a String array, or null if the parameter was not sent. The array has one entry for each parameter field sent. If any field was sent without a value that entry is stored in the array as a null. The values are guaranteed to be in their normal, decoded form. A single value is returned as a one-element array.

Parameters:
name - the parameter name.
Returns:
the parameter values.

getFilesystemName

public java.lang.String getFilesystemName(java.lang.String name)
Returns the filesystem name of the specified file, or null if the file was not included in the upload. A filesystem name is the name specified by the user. It is also the name under which the file is actually saved.

Parameters:
name - the file name.
Returns:
the filesystem name of the file.

getOriginalFileName

public java.lang.String getOriginalFileName(java.lang.String name)
Returns the original filesystem name of the specified file (before any renaming policy was applied), or null if the file was not included in the upload. A filesystem name is the name specified by the user.

Parameters:
name - the file name.
Returns:
the original file name of the file.

getContentType

public java.lang.String getContentType(java.lang.String name)
Returns the content type of the specified file (as supplied by the client browser), or null if the file was not included in the upload.

Parameters:
name - the file name.
Returns:
the content type of the file.

getFile

public java.io.File getFile(java.lang.String name)
Returns a File object for the specified file saved on the server's filesystem, or null if the file was not included in the upload.

Parameters:
name - the file name.
Returns:
a File object for the named file.