public class MultipartParser
extends java.lang.Object
multipart/form-data
requests,
the kind of requests that support file uploads. This class uses a
"pull" model where the reading of incoming files and parameters is
controlled by the client code, which allows incoming files to be stored
into any OutputStream
. If you wish to use an API which
resembles HttpServletRequest
, use the "push" model
MultipartRequest
instead. It's an easy-to-use wrapper
around this class.
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).
It also optionally includes enhanced buffering and Content-Length limitation. Buffering is only required if your servlet container is poorly implemented (many are, including Tomcat 3.2), but it is generally recommended because it will make a slow servlet container a lot faster, and will only make a fast servlet container a little slower. Content-Length limiting is usually only required if you find that your servlet is hanging trying to read the input stram from the POST, and it is similarly recommended because it only has a minimal impact on performance.
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.
MultipartRequest
Constructor and Description |
---|
MultipartParser(javax.servlet.http.HttpServletRequest req,
int maxSize)
Creates a
MultipartParser from the specified request,
which limits the upload size to the specified length, buffers for
performance and prevent attempts to read past the amount specified
by the Content-Length. |
MultipartParser(javax.servlet.http.HttpServletRequest req,
int maxSize,
boolean buffer,
boolean limitLength)
Creates a
MultipartParser from the specified request,
which limits the upload size to the specified length, and optionally
buffers for performance and prevents attempts to read past the amount
specified by the Content-Length. |
MultipartParser(javax.servlet.http.HttpServletRequest req,
int maxSize,
boolean buffer,
boolean limitLength,
java.lang.String encoding)
Creates a
MultipartParser from the specified request,
which limits the upload size to the specified length, and optionally
buffers for performance and prevents attempts to read past the amount
specified by the Content-Length, and with a specified encoding. |
Modifier and Type | Method and Description |
---|---|
Part |
readNextPart()
Read the next part arriving in the stream.
|
void |
setEncoding(java.lang.String encoding)
Sets the encoding used to parse from here onward.
|
public MultipartParser(javax.servlet.http.HttpServletRequest req, int maxSize) throws java.io.IOException
MultipartParser
from the specified request,
which limits the upload size to the specified length, buffers for
performance and prevent attempts to read past the amount specified
by the Content-Length.req
- the servlet request.maxSize
- the maximum size of the POST content.java.io.IOException
public MultipartParser(javax.servlet.http.HttpServletRequest req, int maxSize, boolean buffer, boolean limitLength) throws java.io.IOException
MultipartParser
from the specified request,
which limits the upload size to the specified length, and optionally
buffers for performance and prevents attempts to read past the amount
specified by the Content-Length.req
- the servlet request.maxSize
- the maximum size of the POST content.buffer
- whether to do internal buffering or let the server buffer,
useful for servers that don't bufferlimitLength
- boolean flag to indicate if we need to filter
the request's input stream to prevent trying to
read past the end of the stream.java.io.IOException
public MultipartParser(javax.servlet.http.HttpServletRequest req, int maxSize, boolean buffer, boolean limitLength, java.lang.String encoding) throws java.io.IOException
MultipartParser
from the specified request,
which limits the upload size to the specified length, and optionally
buffers for performance and prevents attempts to read past the amount
specified by the Content-Length, and with a specified encoding.req
- the servlet request.maxSize
- the maximum size of the POST content.buffer
- whether to do internal buffering or let the server buffer,
useful for servers that don't bufferlimitLength
- boolean flag to indicate if we need to filter
the request's input stream to prevent trying to
read past the end of the stream.encoding
- the encoding to use for parsing, default is ISO-8859-1.java.io.IOException
public void setEncoding(java.lang.String encoding)
encoding
- The encoding to use for parsingpublic Part readNextPart() throws java.io.IOException
FilePart
or a ParamPart
, or null
to indicate there are no more parts to read. The order of arrival
corresponds to the order of the form elements in the submitted form.