com.oreilly.servlet.multipart
Class MultipartParser

java.lang.Object
  extended by com.oreilly.servlet.multipart.MultipartParser

public class MultipartParser
extends java.lang.Object

A utility class to handle 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.

Version:
1.11, 2002/11/01, added constructor that takes an encoding, to make sure chars are always read correctly, 1.10, 2002/11/01, added support for a preamble before the first boundary marker, 1.9, 2002/11/01, added support to parse odd Opera Content-Type, 1.8, 2002/11/01, added support for lynx with unquoted param vals, 1.7, 2002/04/30, fixed bug if a line was '\n' alone, 1.6, 2002/04/30, added better internationalization support, thanks to Changshin Lee, 1.5, 2002/04/30, added Opera header fix, thanks to Nic Ferrier, 1.4, 2001/03/23, added IE5 bug workaround supporting \n as line ending, thanks to Michael Alyn Miller, 1.3, 2001/01/22, added support for boundaries surrounded by quotes and content-disposition after content-type, thanks to Scott Stark, 1.2, 2001/01/22, getFilePath() support thanks to Stefan Eissing, 1.1, 2000/10/29, integrating old WebSphere fix, 1.0, 2000/10/27, initial revision
Author:
Jason Hunter, Geoff Soutter
See Also:
MultipartRequest

Constructor Summary
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, 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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultipartParser

public MultipartParser(HttpServletRequest req,
                       int maxSize)
                throws java.io.IOException
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.

Parameters:
req - the servlet request.
maxSize - the maximum size of the POST content.
Throws:
java.io.IOException

MultipartParser

public MultipartParser(HttpServletRequest req,
                       int maxSize,
                       boolean buffer,
                       boolean limitLength)
                throws java.io.IOException
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.

Parameters:
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 buffer
limitLength - 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.
Throws:
java.io.IOException

MultipartParser

public MultipartParser(HttpServletRequest req,
                       int maxSize,
                       boolean buffer,
                       boolean limitLength,
                       java.lang.String encoding)
                throws java.io.IOException
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.

Parameters:
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 buffer
limitLength - 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.
Throws:
java.io.IOException
Method Detail

setEncoding

public void setEncoding(java.lang.String encoding)
Sets the encoding used to parse from here onward. The default is ISO-8859-1. Encodings are actually best passed into the contructor, so even the initial line reads are correct.

Parameters:
encoding - The encoding to use for parsing

readNextPart

public Part readNextPart()
                  throws java.io.IOException
Read the next part arriving in the stream. Will be either a 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.

Returns:
either a FilePart, a ParamPart or null if there are no more parts to read.
Throws:
java.io.IOException - if an input or output exception has occurred.
See Also:
FilePart, ParamPart