public class MultipartParser extends 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
Modifier and Type | Field and Description |
---|---|
private String |
boundary
MIME boundary that delimits parts
|
private byte[] |
buf
buffer for readLine method
|
private static String |
DEFAULT_ENCODING
default encoding
|
private String |
encoding
preferred encoding
|
private ServletInputStream |
in
input stream to read parts from
|
private FilePart |
lastFilePart
reference to the last file part we returned
|
Constructor and Description |
---|
MultipartParser(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(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(HttpServletRequest req,
int maxSize,
boolean buffer,
boolean limitLength,
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 |
---|---|
private String |
extractBoundary(String line)
Extracts and returns the boundary token from a line.
|
private static String |
extractContentType(String line)
Extracts and returns the content type from a line, or null if the
line was empty.
|
private String[] |
extractDispositionInfo(String line)
Extracts and returns disposition info from a line, as a
String |
private String |
readLine()
Read the next line of input.
|
Part |
readNextPart()
Read the next part arriving in the stream.
|
void |
setEncoding(String encoding)
Sets the encoding used to parse from here onward.
|
private ServletInputStream in
private FilePart lastFilePart
private byte[] buf
private static String DEFAULT_ENCODING
public MultipartParser(HttpServletRequest req, int maxSize) throws 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.IOException
public MultipartParser(HttpServletRequest req, int maxSize, boolean buffer, boolean limitLength) throws 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.IOException
public MultipartParser(HttpServletRequest req, int maxSize, boolean buffer, boolean limitLength, String encoding) throws 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.IOException
public void setEncoding(String encoding)
encoding
- The encoding to use for parsingpublic Part readNextPart() throws 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.FilePart
, a ParamPart
or
null
if there are no more parts to read.IOException
- if an input or output exception has occurred.FilePart
,
ParamPart
private String extractBoundary(String line)
private String[] extractDispositionInfo(String line) throws IOException
String
array with elements: disposition, name, filename.
IOException
- if the line is malformatted.private static String extractContentType(String line) throws IOException
IOException
- if the line is malformatted.private String readLine() throws IOException
IOException
- if an input or output exception has occurred.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses