org.apache.struts.upload

Class MultipartIterator

public class MultipartIterator extends Object

Deprecated: Use the Commons FileUpload based multipart handler instead. This class will be removed after Struts 1.2.

The MultipartIterator class is responsible for reading the input data of a multipart request and splitting it up into input elements, wrapped inside of a {@link org.apache.struts.upload.MultipartElement MultipartElement} for easy definition. To use this class, create a new instance of MultipartIterator passing it a HttpServletRequest in the constructor. Then use the {@link #getNextElement() getNextElement} method until it returns null, then you're finished. Example:
      MultipartIterator iterator = new MultipartIterator(request);
      MultipartElement element;

      while ((element = iterator.getNextElement()) != null) {
           //do something with element
      }
 

See Also:

Field Summary
protected Stringboundary
The boundary for this multipart request
protected intbufferSize
The amount of data read from a request at a time.
protected intcontentLength
The content length of this request
protected StringcontentType
The content-type.
protected intdiskBufferSize
The size in bytes written to the filesystem at a time [20K]
static StringDEFAULT_ENCODING
The default encoding of a text element if none is specified.
static StringFILE_PREFIX
static StringHEADER_CONTENT_DISPOSITION
The name of the Content-Disposition header.
static StringHEADER_CONTENT_TYPE
The name of the Content-Type header.
protected MultipartBoundaryInputStreaminputStream
The InputStream to use to read the multipart data.
protected booleanmaxLengthExceeded
Whether the maximum length has been exceeded.
protected longmaxSize
The maximum file size in bytes allowed.
static StringMESSAGE_CANNOT_RETRIEVE_BOUNDARY
The exception message for when the boundary of a multipart request can't be determined.
static StringPARAMETER_BOUNDARY
protected HttpServletRequestrequest
The request instance for this class
protected StringtempDir
The temporary directory to store files
static intTEXT_BUFFER_SIZE
The size in bytes to copy of text data at a time.
Constructor Summary
MultipartIterator(HttpServletRequest request)
Constructs a MultipartIterator with a default buffer size and no file size limit
MultipartIterator(HttpServletRequest request, int bufferSize)
Constructs a MultipartIterator with the specified buffer size and no file size limit
MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize)
Constructs a MultipartIterator with the specified buffer size and the specified file size limit in bytes
MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize, String tempDir)
Method Summary
protected MultipartElementcreateFileMultipartElement()
Create a multipart element instance representing the file in the stream.
protected FilecreateLocalFile()
Creates a file on disk from the current mulitpart element.
protected MultipartElementcreateTextMultipartElement(String encoding)
Create a text element from the data in the body of the element.
voidgetBoundaryFromContentType()
Parses a content-type String for the boundary.
intgetBufferSize()
Get the maximum amount of bytes read from a line at one time
voidgetContentTypeOfRequest()
Gets the value of the Content-Type header of the request.
protected StringgetElementEncoding()
Get the character encoding used for this current multipart element.
longgetMaxSize()
Get the maximum post data size allowed for a multipart request
MultipartElementgetNextElement()
Retrieves the next element in the iterator if one exists.
booleanisMaxLengthExceeded()
Whether or not the maximum length has been exceeded by the client.
protected voidparseRequest()
Handles retrieving the boundary and setting the input stream
voidsetBufferSize(int bufferSize)
Set the maximum amount of bytes read from a line at one time
voidsetMaxSize(long maxSize)
Set the maximum post data size allowed for a multipart request

Field Detail

boundary

protected String boundary
The boundary for this multipart request

bufferSize

protected int bufferSize
The amount of data read from a request at a time. This also represents the maximum size in bytes of a line read from the request [4KB]

contentLength

protected int contentLength
The content length of this request

contentType

protected String contentType
The content-type.

diskBufferSize

protected int diskBufferSize
The size in bytes written to the filesystem at a time [20K]

DEFAULT_ENCODING

private static final String DEFAULT_ENCODING
The default encoding of a text element if none is specified.

FILE_PREFIX

private static final String FILE_PREFIX

HEADER_CONTENT_DISPOSITION

public static final String HEADER_CONTENT_DISPOSITION
The name of the Content-Disposition header.

HEADER_CONTENT_TYPE

public static String HEADER_CONTENT_TYPE
The name of the Content-Type header.

inputStream

protected MultipartBoundaryInputStream inputStream
The InputStream to use to read the multipart data.

maxLengthExceeded

protected boolean maxLengthExceeded
Whether the maximum length has been exceeded.

maxSize

protected long maxSize
The maximum file size in bytes allowed. Ignored if -1

MESSAGE_CANNOT_RETRIEVE_BOUNDARY

public static final String MESSAGE_CANNOT_RETRIEVE_BOUNDARY
The exception message for when the boundary of a multipart request can't be determined.

PARAMETER_BOUNDARY

private static final String PARAMETER_BOUNDARY

request

protected HttpServletRequest request
The request instance for this class

tempDir

protected String tempDir
The temporary directory to store files

TEXT_BUFFER_SIZE

private static final int TEXT_BUFFER_SIZE
The size in bytes to copy of text data at a time.

Constructor Detail

MultipartIterator

public MultipartIterator(HttpServletRequest request)
Constructs a MultipartIterator with a default buffer size and no file size limit

Parameters: request The multipart request to iterate

MultipartIterator

public MultipartIterator(HttpServletRequest request, int bufferSize)
Constructs a MultipartIterator with the specified buffer size and no file size limit

Parameters: request The multipart request to iterate bufferSize The size in bytes that should be read from the input stream at a times

MultipartIterator

public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize)
Constructs a MultipartIterator with the specified buffer size and the specified file size limit in bytes

Parameters: request The multipart request to iterate bufferSize The size in bytes that should be read from the input stream at a times maxSize The maximum size in bytes allowed for a multipart element's data

MultipartIterator

public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize, String tempDir)

Method Detail

createFileMultipartElement

protected MultipartElement createFileMultipartElement()
Create a multipart element instance representing the file in the stream.

createLocalFile

protected File createLocalFile()
Creates a file on disk from the current mulitpart element.

createTextMultipartElement

protected MultipartElement createTextMultipartElement(String encoding)
Create a text element from the data in the body of the element.

Parameters: encoding The character encoding of the string.

getBoundaryFromContentType

private final void getBoundaryFromContentType()
Parses a content-type String for the boundary.

getBufferSize

public int getBufferSize()
Get the maximum amount of bytes read from a line at one time

See Also: javax.servlet.ServletInputStream#readLine(byte[], int, int)

getContentTypeOfRequest

private final void getContentTypeOfRequest()
Gets the value of the Content-Type header of the request.

getElementEncoding

protected String getElementEncoding()
Get the character encoding used for this current multipart element.

getMaxSize

public long getMaxSize()
Get the maximum post data size allowed for a multipart request

Returns: The maximum post data size in bytes

getNextElement

public MultipartElement getNextElement()
Retrieves the next element in the iterator if one exists.

Returns: a {@link org.apache.struts.upload.MultipartElement MultipartElement} representing the next element in the request data

Throws: IOException if the post size exceeds the maximum file size passed in the 3 argument constructor or if the "ISO-8859-1" encoding isn't found

isMaxLengthExceeded

public boolean isMaxLengthExceeded()
Whether or not the maximum length has been exceeded by the client.

parseRequest

protected void parseRequest()
Handles retrieving the boundary and setting the input stream

setBufferSize

public void setBufferSize(int bufferSize)
Set the maximum amount of bytes read from a line at one time

See Also: javax.servlet.ServletInputStream#readLine(byte[], int, int)

setMaxSize

public void setMaxSize(long maxSize)
Set the maximum post data size allowed for a multipart request

Parameters: maxSize The maximum post data size in bytes, set to -1 for no limit

Copyright B) 2000-2007 - The Apache Software Foundation