Class MultipartRequest

A Multipart form data parser. Parses an input stream and writes out any files found, making available a hashtable of other url parameters. As of version 1.17 the files can be saved to memory, and optionally written to a database, etc.

Copyright (C)2001 Jason Pell.

        This library is free software; you can redistribute it and/or
        modify it under the terms of the GNU Lesser General Public
        License as published by the Free Software Foundation; either
        version 2.1 of the License, or (at your option) any later version.
        


        This library is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
        Lesser General Public License for more details.
        


        You should have received a copy of the GNU Lesser General Public
        License along with this library; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-130\
7  USA
        

	
        Email: 	jasonpell@hotmail.com
        Url:	http://www.geocities.com/jasonpell
        
        

Synopsis

Inheritance Path.  java.lang.Object-> com.itmill.toolkit.terminal.web.MultipartRequest

MultipartRequest(PrintWriter, String, int, InputStream, int)

Parameters

debug

A PrintWriter that can be used for debugging.

strContentTypeText

The "Content-Type" HTTP header value.

intContentLength

The "Content-Length" HTTP header value.

in

The InputStream to read and parse.

intMaxReadBytes

Overrides the MAX_BYTES_READ value, to allow arbitrarily long files.

Exceptions

IllegalArgumentException

If the strContentTypeText does not contain a Content-Type of "multipart/form-data" or the boundary is not found.

IOException

If the intContentLength is higher than MAX_READ_BYTES or strSaveDirectory is invalid or cannot be written to.

See Also
MAX_READ_BYTES

Constructor - load into memory constructor

MultipartRequest(PrintWriter, String, int, InputStream, String)

Parameters

debug

A PrintWriter that can be used for debugging.

strContentTypeText

The "Content-Type" HTTP header value.

intContentLength

The "Content-Length" HTTP header value.

in

The InputStream to read and parse.

strSaveDirectory

The temporary directory to save the file from where they can then be moved to wherever by the calling process. If you specify null for this parameter, then any files uploaded will be silently ignored.

Exceptions

IllegalArgumentException

If the strContentTypeText does not contain a Content-Type of "multipart/form-data" or the boundary is not found.

IOException

If the intContentLength is higher than MAX_READ_BYTES or strSaveDirectory is invalid or cannot be written to.

See Also
MAX_READ_BYTES

Deprecated

Replaced by MultipartRequest(PrintWriter, String, int, InputStream, int) You can specify MultipartRequest.MAX_READ_BYTES for the intMaxReadBytes parameter

Constructor.

MultipartRequest(PrintWriter, String, int, InputStream, String, int)

Parameters

debug

A PrintWriter that can be used for debugging.

strContentTypeText

The "Content-Type" HTTP header value.

intContentLength

The "Content-Length" HTTP header value.

in

The InputStream to read and parse.

strSaveDirectory

The temporary directory to save the file from where they can then be moved to wherever by the calling process. If you specify null for this parameter, then any files uploaded will be silently ignored.

intMaxReadBytes

Overrides the MAX_BYTES_READ value, to allow arbitrarily long files.

Exceptions

IllegalArgumentException

If the strContentTypeText does not contain a Content-Type of "multipart/form-data" or the boundary is not found.

IOException

If the intContentLength is higher than MAX_READ_BYTES or strSaveDirectory is invalid or cannot be written to.

See Also
MAX_READ_BYTES

Constructor.

MultipartRequest(String, int, InputStream, String)

Parameters

strContentTypeText

The "Content-Type" HTTP header value.

intContentLength

The "Content-Length" HTTP header value.

in

The InputStream to read and parse.

strSaveDirectory

The temporary directory to save the file from where they can then be moved to wherever by the calling process. If you specify null for this parameter, then any files uploaded will be silently ignored.

Exceptions

IllegalArgumentException

If the strContentTypeText does not contain a Content-Type of "multipart/form-data" or the boundary is not found.

IOException

If the intContentLength is higher than MAX_READ_BYTES or strSaveDirectory is invalid or cannot be written to.

See Also
MAX_READ_BYTES

Constructor.

MultipartRequest(String, int, InputStream, String, int)

Parameters

strContentTypeText

The "Content-Type" HTTP header value.

intContentLength

The "Content-Length" HTTP header value.

in

The InputStream to read and parse.

strSaveDirectory

The temporary directory to save the file from where they can then be moved to wherever by the calling process. If you specify null for this parameter, then any files uploaded will be silently ignored.

intMaxReadBytes

Overrides the MAX_BYTES_READ value, to allow arbitrarily long files.

Exceptions

IllegalArgumentException

If the strContentTypeText does not contain a Content-Type of "multipart/form-data" or the boundary is not found.

IOException

If the intContentLength is higher than MAX_READ_BYTES or strSaveDirectory is invalid or cannot be written to.

See Also
MAX_READ_BYTES

Constructor.

CONTENT_TYPE

Type constant for the File CONTENT_TYPE.

CONTENTS

Type constant for the File CONTENTS. Note: Only used for file upload to memory.

FILENAME

Type constant for File FILENAME.

MAX_READ_BYTES

Prevent a denial of service by defining this, will never read more data. If Content-Length is specified to be more than this, will throw an exception. This limits the maximum number of bytes to the value of an int, which is 2 Gigabytes.

READ_LINE_BLOCK

Defines the number of bytes to read per readLine call. 128K

SIZE

Type constant for the File SIZE.

getContentType(String)

Returns the Content-Type of a file.

getEncoding()

Returns the current encoding method.

getFile(String)

Parameters

return

Returns a null file reference if a call to getFileSize(strName) returns zero or files were uploaded to memory.

Returns a File reference to the uploaded file. This reference is to the files uploaded location, and allows you to read/move/delete the file. This method is only of use, if files were uploaded to the file system. Will return null if uploaded to memory, in which case you should use getFileContents(strName) instead.

getFileContents(String)

Parameters

return

the contents of the file as a InputStream, or null if not file uploaded, or file uploaded to file system directory.

If files were uploaded into memory, this method will retrieve the contents of the file as a InputStream.

getFileParameter(String, int)

Parameters

strName

is the form field name, used to upload the file. This identifies the formfield location in the storage facility.

strFilename

This is the FileSystemName of the file

type

What attribute you want from the File Parameter. The following types are supported: MultipartRequest.FILENAME, MultipartRequest.CONTENT_TYPE, MultipartRequest.SIZE, MultipartRequest.CONTENTS

The getFileSystemName(String strName),getFileSize(String strName),getContentType(String strName), getContents(String strName) methods all use this method passing in a different type argument.

Note: This class has been changed to provide for future functionality where you will be able to access all files uploaded, even if they are uploaded using the same form field name. At this point however, only the first file uploaded via a form field name is accessible.

Access an attribute of a file upload parameter record.

getFileParameterNames()

This enumeration will return all INPUT TYPE=FILE parameter NAMES as encountered during the upload.

getFileSize(String)

Parameters

return

-1 if file size not defined.

Returns the File Size of a uploaded file.

getFileSystemName(String)

Parameters

return

null if strName not found.

Get the file system basename of an uploaded file.

getHtmlTable()

For debugging.

getParameterNames()

An enumeration of all URL Parameters for the current HTTP Request.

getURLParameter(String)

Return the value of the strName URLParameter. If more than one value for a particular Parameter, will return the first. If an error occurs will return null.

getURLParameters(String)

Return an enumeration of all values for the strName parameter. Even if a single value for, will always return an enumeration, although it may actually be empty if no value was encountered for strName or it is an invalid parameter name.

setEncoding(String)

This method should be called on the MultipartRequest itself, not on any instances of MultipartRequest, because this sets up the encoding for all instances of multipartrequest. You can set the encoding to null, in which case the default encoding will be applied. The default encoding if this method is not called has been set to ISO-8859-1, which seems to offer the best hope of support for international characters, such as german "Umlaut" characters.

Warning: In multithreaded environments it is the responsibility of the implementer to make sure that this method is not called while another instance is being constructed. When an instance of MultipartRequest is constructed, it parses the input data, and uses the result of getEncoding() to convert between bytes and strings. If setEncoding() is called by another thread, while the private parse() is executing, the method will utilise this new encoding, which may cause serious problems.