org.apache.commons.httpclient.methods

Class MultipartPostMethod

Implemented Interfaces:
HttpMethod

public class MultipartPostMethod
extends ExpectContinueMethod

Implements the HTTP multipart POST method.

The HTTP multipart POST method is defined in section 3.3 of RFC1867:

The media-type multipart/form-data follows the rules of all multipart MIME data streams as outlined in RFC 1521. The multipart/form-data contains a series of parts. Each part is expected to contain a content-disposition header where the value is "form-data" and a name attribute specifies the field name within the form, e.g., 'content-disposition: form-data; name="xxxxx"', where xxxxx is the field name corresponding to that field. Field names originally in non-ASCII character sets may be encoded using the method outlined in RFC 1522.

Authors:
Matthew Albright
Jeff Dever
Adrian Sutton
Mark Diggory
Mike Bowler
Oleg Kalnichevski
Since:
2.0

Field Summary

static String
MULTIPART_FORM_CONTENT_TYPE
The Content-Type for multipart/form-data.

Constructor Summary

MultipartPostMethod()
No-arg constructor.
MultipartPostMethod(String uri)
Constructor specifying a URI.

Method Summary

protected void
addContentLengthRequestHeader(HttpState state, HttpConnection conn)
Adds a Content-Length request header, as long as no Content-Length request header already exists.
protected void
addContentTypeRequestHeader(HttpState state, HttpConnection conn)
Adds a Content-Type request header.
void
addParameter(String parameterName, File parameterFile)
Adds a binary file part
void
addParameter(String parameterName, String parameterValue)
Adds a text field part
void
addParameter(String parameterName, String fileName, File parameterFile)
Adds a binary file part with the given file name
void
addPart(Part part)
Adds a part.
protected void
addRequestHeaders(HttpState state, HttpConnection conn)
Populates the request headers map to with additional headers to be submitted to the given HttpConnection.
String
getName()
Returns "POST".
Part[]
getParts()
Returns all parts.
protected long
getRequestContentLength()
Return the length of the request body.
protected boolean
hasRequestContent()
Returns true
void
recycle()
Deprecated. no longer supported and will be removed in the future version of HttpClient
protected boolean
writeRequestBody(HttpState state, HttpConnection conn)
Writes the request body to the given connection.

Methods inherited from class org.apache.commons.httpclient.methods.ExpectContinueMethod

addRequestHeaders, getUseExpectHeader, hasRequestContent, setUseExpectHeader

Methods inherited from class org.apache.commons.httpclient.HttpMethodBase

abort, addCookieRequestHeader, addHostRequestHeader, addProxyConnectionHeader, addRequestHeader, addRequestHeader, addRequestHeaders, addResponseFooter, addUserAgentRequestHeader, checkNotUsed, checkUsed, execute, generateRequestLine, getAuthenticationRealm, getContentCharSet, getDoAuthentication, getEffectiveVersion, getFollowRedirects, getHostAuthState, getHostConfiguration, getMethodRetryHandler, getName, getParams, getPath, getProxyAuthState, getProxyAuthenticationRealm, getQueryString, getRecoverableExceptionCount, getRequestCharSet, getRequestHeader, getRequestHeaderGroup, getRequestHeaders, getRequestHeaders, getResponseBody, getResponseBodyAsStream, getResponseBodyAsString, getResponseCharSet, getResponseContentLength, getResponseFooter, getResponseFooters, getResponseHeader, getResponseHeaderGroup, getResponseHeaders, getResponseHeaders, getResponseStream, getResponseTrailerHeaderGroup, getStatusCode, getStatusLine, getStatusText, getURI, hasBeenUsed, isAborted, isConnectionCloseForced, isHttp11, isRequestSent, isStrictMode, processResponseBody, processResponseHeaders, processStatusLine, readResponse, readResponseBody, readResponseHeaders, readStatusLine, recycle, releaseConnection, removeRequestHeader, removeRequestHeader, responseBodyConsumed, setConnectionCloseForced, setDoAuthentication, setFollowRedirects, setHostConfiguration, setHttp11, setMethodRetryHandler, setParams, setPath, setQueryString, setQueryString, setRequestHeader, setRequestHeader, setResponseStream, setStrictMode, setURI, shouldCloseConnection, validate, writeRequest, writeRequestBody, writeRequestHeaders, writeRequestLine

Field Details

MULTIPART_FORM_CONTENT_TYPE

public static final String MULTIPART_FORM_CONTENT_TYPE
The Content-Type for multipart/form-data.

Constructor Details

MultipartPostMethod

public MultipartPostMethod()
No-arg constructor.

MultipartPostMethod

public MultipartPostMethod(String uri)
Constructor specifying a URI.
Parameters:
uri - either an absolute or relative URI

Method Details

addContentLengthRequestHeader

protected void addContentLengthRequestHeader(HttpState state,
                                             HttpConnection conn)
            throws IOException,
                   HttpException
Adds a Content-Length request header, as long as no Content-Length request header already exists.
Parameters:
state - current state of http requests
conn - the connection to use for I/O
Throws:
HttpException - if a protocol exception occurs. Usually protocol exceptions cannot be recovered from.
Since:
3.0

addContentTypeRequestHeader

protected void addContentTypeRequestHeader(HttpState state,
                                           HttpConnection conn)
            throws IOException,
                   HttpException
Adds a Content-Type request header.
Parameters:
state - current state of http requests
conn - the connection to use for I/O
Throws:
HttpException - if a protocol exception occurs. Usually protocol exceptions cannot be recovered from.
Since:
3.0

addParameter

public void addParameter(String parameterName,
                         File parameterFile)
            throws FileNotFoundException
Adds a binary file part
Parameters:
parameterName - The name of the parameter
parameterFile - The name of the file.

addParameter

public void addParameter(String parameterName,
                         String parameterValue)
Adds a text field part
Parameters:
parameterName - The name of the parameter.
parameterValue - The value of the parameter.

addParameter

public void addParameter(String parameterName,
                         String fileName,
                         File parameterFile)
            throws FileNotFoundException
Adds a binary file part with the given file name
Parameters:
parameterName - The name of the parameter
fileName - The file name
parameterFile - The file

addPart

public void addPart(Part part)
Adds a part.
Parameters:
part - The part to add.

addRequestHeaders

protected void addRequestHeaders(HttpState state,
                                 HttpConnection conn)
            throws IOException,
                   HttpException
Populates the request headers map to with additional headers to be submitted to the given HttpConnection.

This implementation adds tt>Content-Length and Content-Type headers, when appropriate.

Subclasses may want to override this method to to add additional headers, and may choose to invoke this implementation (via super) to add the "standard" headers.

Overrides:
addRequestHeaders in interface ExpectContinueMethod
Parameters:
state - the state information associated with this method
conn - the connection used to execute this HTTP method
Throws:
HttpException - if a protocol exception occurs. Usually protocol exceptions cannot be recovered from.
See Also:
MultipartPostMethod

getName

public String getName()
Returns "POST".
Specified by:
getName in interface HttpMethod
Overrides:
getName in interface HttpMethodBase
Returns:
"POST"

getParts

public Part[] getParts()
Returns all parts.
Returns:
an array of containing all parts

getRequestContentLength

protected long getRequestContentLength()
            throws IOException
Returns:
The request content length.

hasRequestContent

protected boolean hasRequestContent()
Returns true
Overrides:
hasRequestContent in interface ExpectContinueMethod
Returns:
true
Since:
2.0beta1

recycle

public void recycle()

Deprecated. no longer supported and will be removed in the future version of HttpClient

Recycles the HTTP method so that it can be used again. Note that all of the instance variables will be reset once this method has been called. This method will also release the connection being used by this HTTP method.
Specified by:
recycle in interface HttpMethod
Overrides:
recycle in interface HttpMethodBase

writeRequestBody

protected boolean writeRequestBody(HttpState state,
                                   HttpConnection conn)
            throws IOException,
                   HttpException
Writes the request body to the given connection.
Overrides:
writeRequestBody in interface HttpMethodBase
Parameters:
state - the state information associated with this method
conn - the connection used to execute this HTTP method
Returns:
true
Throws:
HttpException - if a protocol exception occurs. Usually protocol exceptions cannot be recovered from.

Copyright (c) 1999-2005 - Apache Software Foundation