public class MultipartRequest
extends java.lang.Object
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.
MultipartParser
Modifier and Type | Field and Description |
---|---|
protected java.util.Hashtable |
files |
protected java.util.Hashtable |
parameters |
Constructor and Description |
---|
MultipartRequest(javax.servlet.http.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(javax.servlet.http.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(javax.servlet.http.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(javax.servlet.http.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(javax.servlet.http.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(javax.servlet.http.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.
|
MultipartRequest(javax.servlet.ServletRequest request,
java.lang.String saveDirectory)
Constructor with an old signature, kept for backward compatibility.
|
MultipartRequest(javax.servlet.ServletRequest request,
java.lang.String saveDirectory,
int maxPostSize)
Constructor with an old signature, kept for backward compatibility.
|
Modifier and Type | Method and Description |
---|---|
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.
|
protected java.util.Hashtable parameters
protected java.util.Hashtable files
public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory) throws java.io.IOException
request
- the servlet request.saveDirectory
- the directory in which to save any uploaded files.java.io.IOException
- if the uploaded content is larger than 1 Megabyte
or there's a problem reading or parsing the request.public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize) throws java.io.IOException
request
- the servlet request.saveDirectory
- the directory in which to save any uploaded files.maxPostSize
- the maximum size of the POST content.java.io.IOException
- if the uploaded content is larger than
maxPostSize or there's a problem reading or parsing the request.public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, java.lang.String encoding) throws java.io.IOException
request
- the servlet request.saveDirectory
- the directory in which to save any uploaded files.encoding
- the encoding of the response, such as ISO-8859-1java.io.IOException
- if the uploaded content is larger than
1 Megabyte or there's a problem reading or parsing the request.public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, FileRenamePolicy policy) throws java.io.IOException
request
- the servlet request.saveDirectory
- the directory in which to save any uploaded files.maxPostSize
- the maximum size of the POST content.policy
- the rules for renaming in case of file name collisionsjava.io.IOException
- if the uploaded content is larger than
maxPostSize or there's a problem reading or parsing the request.public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, java.lang.String encoding) throws java.io.IOException
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-1java.io.IOException
- if the uploaded content is larger than
maxPostSize or there's a problem reading or parsing the request.public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, java.lang.String encoding, FileRenamePolicy policy) throws java.io.IOException
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-1policy
- a pluggable file rename policyjava.io.IOException
- if the uploaded content is larger than
maxPostSize or there's a problem reading or parsing the request.public MultipartRequest(javax.servlet.ServletRequest request, java.lang.String saveDirectory) throws java.io.IOException
java.io.IOException
public MultipartRequest(javax.servlet.ServletRequest request, java.lang.String saveDirectory, int maxPostSize) throws java.io.IOException
java.io.IOException
public java.util.Enumeration getParameterNames()
public java.util.Enumeration getFileNames()
public java.lang.String getParameter(java.lang.String name)
name
- the parameter name.public java.lang.String[] getParameterValues(java.lang.String name)
name
- the parameter name.public java.lang.String getFilesystemName(java.lang.String name)
name
- the html page's file parameter name.public java.lang.String getOriginalFileName(java.lang.String name)
name
- the html page's file parameter name.public java.lang.String getContentType(java.lang.String name)
name
- the html page's file parameter name.public java.io.File getFile(java.lang.String name)
name
- the html page's file parameter name.