|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.instantbank.lettertemplate.control.web.handlers.MultipartRequest
A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. 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) or internationalized content (such as non Latin-1 filenames).
It's used like this:
A client can upload files using an HTML form with the following structure. Note that not all browsers support file uploads.MultipartRequest multi = new MultipartRequest(req, "."); out.println("Params:"); Enumeration params = multi.getParameterNames(); while (params.hasMoreElements()) { String name = (String)params.nextElement(); String value = multi.getParameter(name); out.println(name + " = " + value); } out.println(); out.println("Files:"); Enumeration files = multi.getFileNames(); while (files.hasMoreElements()) { String name = (String)files.nextElement(); String filename = multi.getFilesystemName(name); String type = multi.getContentType(name); File f = multi.getFile(name); out.println("name: " + name); out.println("filename: " + filename); out.println("type: " + type); if (f != null) { out.println("f.toString(): " + f.toString()); out.println("f.getName(): " + f.getName()); out.println("f.exists(): " + f.exists()); out.println("f.length(): " + f.length()); out.println(); } }
<FORM ACTION="/servlet/Handler" METHOD=POST ENCTYPE="multipart/form-data"> What is your name? <INPUT TYPE=TEXT NAME=submitter> <BR> Which file to upload? <INPUT TYPE=FILE NAME=file> <BR> <INPUT TYPE=SUBMIT> </FORM>
The full file upload specification is contained in experimental RFC 1867, available at http://www.ietf.org/rfc/rfc1867.txt .
Field Summary | |
private static int |
DEFAULT_MAX_POST_SIZE
|
private java.io.File |
dir
|
private java.util.Hashtable |
files
|
private int |
maxSize
|
private static java.lang.String |
NO_FILE
|
private java.util.Hashtable |
parameters
|
private javax.servlet.http.HttpServletRequest |
req
|
Constructor Summary | |
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.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. |
Method Summary | |
private java.lang.String |
extractBoundary(java.lang.String line)
Extracts and returns the boundary token from a line. |
private java.lang.String |
extractContentType(java.lang.String line)
Extracts and returns the content type from a line, or null if the line was empty. |
private java.lang.String[] |
extractDispositionInfo(java.lang.String line)
Extracts and returns disposition info from a line, as a String array with elements: disposition, name, filename. |
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 |
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 void |
readAndSaveFile(MultipartInputStreamHandler in,
java.lang.String boundary,
java.lang.String filename,
java.lang.String contentType)
A utility method that reads a single part of the multipart request that represents a file, and saves the file to the given directory. |
protected boolean |
readNextPart(MultipartInputStreamHandler in,
java.lang.String boundary)
A utility method that reads an individual part. |
protected java.lang.String |
readParameter(MultipartInputStreamHandler in,
java.lang.String boundary)
A utility method that reads a single part of the multipart request that represents a parameter. |
protected void |
readRequest()
The workhorse method that actually parses the request. |
Methods inherited from class java.lang.Object |
|
Field Detail |
private static final int DEFAULT_MAX_POST_SIZE
private static final java.lang.String NO_FILE
private javax.servlet.http.HttpServletRequest req
private java.io.File dir
private int maxSize
private java.util.Hashtable parameters
private java.util.Hashtable files
Constructor Detail |
public MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory) throws java.io.IOException
request
- the servlet requestsaveDirectory
- the directory in which to save any uploaded filesjava.io.IOException
- Description of the Exceptionpublic MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize) throws java.io.IOException
request
- the servlet requestsaveDirectory
- the directory in which to save any uploaded filesmaxPostSize
- the maximum size of the POST contentjava.io.IOException
- Description of the Exceptionpublic MultipartRequest(javax.servlet.ServletRequest request, java.lang.String saveDirectory) throws java.io.IOException
request
- Description of the ParametersaveDirectory
- Description of the Parameterjava.io.IOException
- Description of the Exceptionpublic MultipartRequest(javax.servlet.ServletRequest request, java.lang.String saveDirectory, int maxPostSize) throws java.io.IOException
request
- Description of the ParametersaveDirectory
- Description of the ParametermaxPostSize
- Description of the Parameterjava.io.IOException
- Description of the ExceptionMethod Detail |
public java.util.Enumeration getParameterNames()
public java.util.Enumeration getFileNames()
public java.lang.String getParameter(java.lang.String name)
name
- the parameter namepublic java.lang.String[] getParameterValues(java.lang.String name)
name
- the parameter namepublic java.lang.String getFilesystemName(java.lang.String name)
name
- the file namepublic java.lang.String getContentType(java.lang.String name)
name
- the file namepublic java.io.File getFile(java.lang.String name)
name
- the file nameprotected void readRequest() throws java.io.IOException
java.io.IOException
- if the uploaded content is larger than maxSize
or there's a problem parsing the requestprotected boolean readNextPart(MultipartInputStreamHandler in, java.lang.String boundary) throws java.io.IOException
readParameter()
and
readAndSaveFile()
methods to do the actual work. A subclass can override this method for a better optimized or differently behaved implementation.
in
- the stream from which to read the partboundary
- the boundary separating partsjava.io.IOException
- if there's a problem reading or parsing the
request.protected java.lang.String readParameter(MultipartInputStreamHandler in, java.lang.String boundary) throws java.io.IOException
in
- the stream from which to read the parameter
informationboundary
- the boundary signifying the end of this partjava.io.IOException
- if there's a problem reading or parsing the requestprotected void readAndSaveFile(MultipartInputStreamHandler in, java.lang.String boundary, java.lang.String filename, java.lang.String contentType) throws java.io.IOException
in
- the stream from which to read the fileboundary
- the boundary signifying the end of this partfilename
- the name under which to save the uploaded filecontentType
- Description of the Parameterjava.io.IOException
- if there's a problem reading or parsing the requestprivate java.lang.String extractBoundary(java.lang.String line)
line
- Description of the Parameterprivate java.lang.String[] extractDispositionInfo(java.lang.String line) throws java.io.IOException
line
- Description of the Parameterjava.io.IOException
- Description of the Exceptionprivate java.lang.String extractContentType(java.lang.String line) throws java.io.IOException
line
- Description of the Parameterjava.io.IOException
- Description of the Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |