org.apache.commons.httpclient.methods
Class MultipartPostMethod

java.lang.Object
  |
  +--org.apache.commons.httpclient.HttpMethodBase
        |
        +--org.apache.commons.httpclient.methods.GetMethod
              |
              +--org.apache.commons.httpclient.methods.ExpectContinueMethod
                    |
                    +--org.apache.commons.httpclient.methods.MultipartPostMethod

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.

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

Field Summary
static java.lang.String MULTIPART_FORM_CONTENT_TYPE
          The Content-Type for multipart/form-data.
 
Fields inherited from class org.apache.commons.httpclient.HttpMethodBase
USER_AGENT
 
Constructor Summary
MultipartPostMethod()
          No-arg constructor.
MultipartPostMethod(java.lang.String uri)
          Constructor specifying a URI.
MultipartPostMethod(java.lang.String uri, java.lang.String tempDir)
          Constructor specifying a URI and tempDir.
MultipartPostMethod(java.lang.String uri, java.lang.String tempDir, java.lang.String tempFile)
          Constructor specifying a URI, tempDir and tempFile.
 
Method Summary
 void addParameter(java.lang.String parameterName, java.io.File parameterFile)
          Adds a binary file part
 void addParameter(java.lang.String parameterName, java.lang.String parameterValue)
          Adds a text field part
 void addParameter(java.lang.String parameterName, java.lang.String fileName, java.io.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)
          Adds Content Type: multipart/form-data header in addition to the "standard" set of headers
 java.lang.String getName()
          Returns "POST".
 Part[] getParts()
          Returns all parts.
protected  int getRequestContentLength()
          Return the length of the request body.
protected  boolean hasRequestContent()
          Returns true
 void recycle()
          Recycles the HTTP method so that it can be used again.
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
getUseExpectHeader, setUseExpectHeader
 
Methods inherited from class org.apache.commons.httpclient.methods.GetMethod
getFileData, getTempDir, getTempFile, getUseDisk, readResponseBody, setFileData, setTempDir, setTempFile, setUseDisk
 
Methods inherited from class org.apache.commons.httpclient.HttpMethodBase
addAuthorizationRequestHeader, addContentLengthRequestHeader, addCookieRequestHeader, addHostRequestHeader, addProxyAuthorizationRequestHeader, addProxyConnectionHeader, addRequestHeader, addRequestHeader, addResponseFooter, addUserAgentRequestHeader, checkNotUsed, checkUsed, execute, fakeResponse, generateRequestLine, getAuthenticationRealm, getContentCharSet, getDoAuthentication, getFollowRedirects, getHostConfiguration, getMethodRetryHandler, getPath, getProxyAuthenticationRealm, getQueryString, getRecoverableExceptionCount, getRequestCharSet, getRequestHeader, getRequestHeaderGroup, getRequestHeaders, getResponseBody, getResponseBodyAsStream, getResponseBodyAsString, getResponseCharSet, getResponseContentLength, getResponseFooter, getResponseFooters, getResponseHeader, getResponseHeaderGroup, getResponseHeaders, getResponseStream, getResponseTrailerHeaderGroup, getStatusCode, getStatusLine, getStatusText, getURI, hasBeenUsed, isConnectionCloseForced, isHttp11, isStrictMode, processResponseBody, processResponseHeaders, processStatusLine, readResponse, readResponseHeaders, readStatusLine, releaseConnection, removeRequestHeader, responseBodyConsumed, setConnectionCloseForced, setDoAuthentication, setFollowRedirects, setHostConfiguration, setHttp11, setMethodRetryHandler, setPath, setQueryString, setQueryString, setRequestHeader, setRequestHeader, setResponseStream, setStrictMode, shouldCloseConnection, validate, writeRequest, writeRequestHeaders, writeRequestLine
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MULTIPART_FORM_CONTENT_TYPE

public static final java.lang.String MULTIPART_FORM_CONTENT_TYPE
The Content-Type for multipart/form-data.
Constructor Detail

MultipartPostMethod

public MultipartPostMethod()
No-arg constructor.

MultipartPostMethod

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

MultipartPostMethod

public MultipartPostMethod(java.lang.String uri,
                           java.lang.String tempDir)
Constructor specifying a URI and tempDir.
Parameters:
uri - either an absolute or relative URI
tempDir - directory to store temp files in

MultipartPostMethod

public MultipartPostMethod(java.lang.String uri,
                           java.lang.String tempDir,
                           java.lang.String tempFile)
Constructor specifying a URI, tempDir and tempFile.
Parameters:
uri - either an absolute or relative URI
tempDir - directory to store temp files in
tempFile - file to store temporary data in
Method Detail

hasRequestContent

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

getName

public java.lang.String getName()
Returns "POST".
Overrides:
getName in class GetMethod
Returns:
"POST"

addParameter

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

addParameter

public void addParameter(java.lang.String parameterName,
                         java.io.File parameterFile)
                  throws java.io.FileNotFoundException
Adds a binary file part
Parameters:
parameterName - The name of the parameter
parameterFile - The name of the file.
Throws:
java.io.FileNotFoundException - If the file cannot be found.

addParameter

public void addParameter(java.lang.String parameterName,
                         java.lang.String fileName,
                         java.io.File parameterFile)
                  throws java.io.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
Throws:
java.io.FileNotFoundException - If the file cannot be found.

addPart

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

getParts

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

addRequestHeaders

protected void addRequestHeaders(HttpState state,
                                 HttpConnection conn)
                          throws java.io.IOException,
                                 HttpException
Adds Content Type: multipart/form-data header in addition to the "standard" set of headers
Overrides:
addRequestHeaders in class ExpectContinueMethod
Parameters:
state - the state information associated with this method
conn - the connection used to execute this HTTP method
Throws:
java.io.IOException - if an I/O (transport) error occurs
HttpException - if a protocol exception occurs.
HttpRecoverableException - if a recoverable transport error occurs. Usually this kind of exceptions can be recovered from by retrying the HTTP method

writeRequestBody

protected boolean writeRequestBody(HttpState state,
                                   HttpConnection conn)
                            throws java.io.IOException,
                                   HttpException
Writes the request body to the given connection.
Overrides:
writeRequestBody in class HttpMethodBase
Parameters:
state - the state information associated with this method
conn - the connection used to execute this HTTP method
Returns:
true
Throws:
java.io.IOException - if an I/O (transport) error occurs
HttpException - if a protocol exception occurs.
HttpRecoverableException - if a recoverable transport error occurs. Usually this kind of exceptions can be recovered from by retrying the HTTP method

getRequestContentLength

protected int getRequestContentLength()

Return the length of the request body.

Once this method has been invoked, the request parameters cannot be altered until the method is recycled.

Overrides:
getRequestContentLength in class HttpMethodBase
Returns:
The request content length.

recycle

public void recycle()
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.
Overrides:
recycle in class GetMethod
See Also:
HttpMethodBase.releaseConnection()


Copyright © 2001-2004 Apache Software Foundation. All Rights Reserved.