public class MultipartRequest extends ConnectionRequest
A multipart post request allows a developer to submit large binary data
files to the server in a multipart mime post request. This is a standard method
for large binary file uploads to webservers and data services.
The sample code below includes both the client code using the upload capabilities as
well as a simple sample servlet that can accept multipart data:
The sample code below demonstrates uploading to the filestack.com API.
ConnectionRequest.CachingMode, ConnectionRequest.SSLCertificate
PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_REDUNDANT
Constructor and Description |
---|
MultipartRequest()
Initialize variables
|
Modifier and Type | Method and Description |
---|---|
void |
addArgument(String name,
String value)
Add an argument to the request response
|
void |
addArgument(String name,
String[] value)
Add an argument to the request response as an array of elements, this will
trigger multiple request entries with the same key
|
void |
addArgumentNoEncoding(String key,
String value)
Add an argument to the request response without encoding it, this is useful for
arguments which are already encoded
|
void |
addArgumentNoEncoding(String key,
String[] value)
Add an argument to the request response as an array of elements, this will
trigger multiple request entries with the same key, notice that this doesn't implicitly
encode the value
|
void |
addArgumentNoEncodingArray(String key,
String... value)
Add an argument to the request response as an array of elements, this will
trigger multiple request entries with the same key, notice that this doesn't implicitly
encode the value
|
void |
addData(String name,
byte[] data,
String mimeType)
Adds a binary argument to the arguments
|
void |
addData(String name,
InputStream data,
long dataSize,
String mimeType)
Adds a binary argument to the arguments, notice the input stream will be
read only during submission
|
void |
addData(String name,
String filePath,
String mimeType)
Adds a binary argument to the arguments
|
protected void |
buildRequestBody(OutputStream os)
Invoked when send body is true, by default sends the request arguments based
on "POST" conventions
|
protected long |
calculateContentLength() |
String |
getBoundary()
Returns the boundary string which is normally generated based on system time
|
int |
getContentLength()
Returns the content length header value
|
protected void |
initConnection(Object connection)
Invoked to initialize HTTP headers, cookies etc.
|
boolean |
isBase64Binaries()
Set to true to encode binary data as base 64
|
static boolean |
isLeaveInputStreamsOpen()
Special flag to keep input stream files open after they are read
|
boolean |
isManualRedirect()
By default redirect responses (302 etc.) are handled manually in multipart requests
|
boolean |
onRedirect(String url)
This is a callback method that been called when there is a redirect.
|
void |
setBase64Binaries(boolean base64Binaries)
Set to true to encode binary data as base 64
|
void |
setBoundary(String boundary)
Sets the boundary string, normally you don't need this method.
|
static void |
setCanFlushStream(boolean flush)
Sending large files requires flushing the writer once in a while to prevent
Out Of Memory Errors, Some J2ME implementation are not able to flush the
streams causing the upload to fail.
|
void |
setFilename(String arg,
String filename)
Sets the filename for the given argument
|
static void |
setLeaveInputStreamsOpen(boolean aLeaveInputStreamsOpen)
Special flag to keep input stream files open after they are read
|
void |
setManualRedirect(boolean autoRedirect)
By default redirect responses (302 etc.) are handled manually in multipart requests, set this
to false to handle the redirect.
|
addArgument, addArgumentArray, addArguments, addExceptionListener, addRequestHeader, addResponseCodeListener, addResponseListener, cacheUnmodified, canGetSSLCertificates, checkSSLCertificates, cookieReceived, cookieSent, createRequestURL, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, equals, fetchJSON, fetchJSONAsync, fireResponseListener, getCachedData, getCacheMode, getContentType, getCookieHeader, getDefaultCacheMode, getDefaultUserAgent, getDestinationFile, getDestinationStorage, getDisposeOnCompletion, getHeader, getHeaderFieldNames, getHeaders, getHttpMethod, getPriority, getReadTimeout, getRequestBody, getRequestBodyData, getResponseCode, getResponseContentType, getResponseData, getResponseErrorMessage, getResposeCode, getShowOnInit, getSilentRetryCount, getSSLCertificates, getTimeout, getUrl, getUserAgent, getYield, handleErrorResponseCode, handleException, handleIOException, handleRuntimeException, hashCode, hasResponseListeners, initCookieHeader, ioStreamUpdate, isCheckSSLCertificates, isCookiesEnabled, isCookiesEnabledDefault, isDefaultFollowRedirects, isDuplicateSupported, isFailSilently, isFollowRedirects, isHandleErrorCodesInGlobalErrorHandler, isInsecure, isKilled, isNativeCookieSharingSupported, isPausable, isPaused, isPost, isReadRequest, isReadResponseForErrors, isReadResponseForErrorsDefault, isReadTimeoutSupported, isRedirecting, isWriteRequest, kill, pause, postResponse, purgeCache, purgeCacheDirectory, readErrorCodeHeaders, readHeaders, readResponse, removeAllArguments, removeArgument, removeExceptionListener, removeResponseCodeListener, removeResponseListener, resume, retry, setCacheMode, setCheckSSLCertificates, setChunkedStreamingMode, setContentType, setCookieHeader, setCookiesEnabled, setCookiesEnabledDefault, setDefaultCacheMode, setDefaultFollowRedirects, setDefaultUserAgent, setDestinationFile, setDestinationStorage, setDisposeOnCompletion, setDuplicateSupported, setFailSilently, setFollowRedirects, setHandleErrorCodesInGlobalErrorHandler, setHttpMethod, setInsecure, setKilled, setPaused, setPost, setPriority, setReadRequest, setReadResponseForErrors, setReadResponseForErrorsDefault, setReadTimeout, setRequestBody, setRequestBody, setShowOnInit, setSilentRetryCount, setTimeout, setUrl, setUseNativeCookieStore, setUserAgent, setWriteRequest, shouldAutoCloseResponse, shouldConvertPostToGetOnRedirect, shouldStop, shouldWriteUTFAsGetBytes, validate
public static boolean isLeaveInputStreamsOpen()
public static void setLeaveInputStreamsOpen(boolean aLeaveInputStreamsOpen)
aLeaveInputStreamsOpen
- the leaveInputStreamsOpen to setpublic String getBoundary()
public void setBoundary(String boundary)
boundary
- the boundary stringprotected void initConnection(Object connection)
ConnectionRequest
initConnection
in class ConnectionRequest
connection
- the connection objectpublic void addData(String name, byte[] data, String mimeType)
name
- the name of the datadata
- the data as bytesmimeType
- the mime type for the contentpublic void addData(String name, String filePath, String mimeType) throws IOException
name
- the name of the file datafilePath
- the path of the file to uploadmimeType
- the mime type for the contentIOException
- if the file cannot be openedpublic void addData(String name, InputStream data, long dataSize, String mimeType)
name
- the name of the datadata
- the data streamdataSize
- the byte size of the data stream, if the data stream is a file
the file size can be obtained using the
FileSystemStorage.getInstance().getLength(file) methodmimeType
- the mime type for the contentpublic void setFilename(String arg, String filename)
arg
- the argument namefilename
- the file namepublic void addArgumentNoEncoding(String key, String value)
addArgumentNoEncoding
in class ConnectionRequest
key
- the key of the argumentvalue
- the value for the argumentpublic void addArgumentNoEncoding(String key, String[] value)
addArgumentNoEncoding
in class ConnectionRequest
key
- the key of the argumentvalue
- the value for the argumentpublic void addArgumentNoEncodingArray(String key, String... value)
addArgumentNoEncodingArray
in class ConnectionRequest
key
- the key of the argumentvalue
- the value for the argumentpublic void addArgument(String name, String[] value)
addArgument
in class ConnectionRequest
name
- the key of the argumentvalue
- the value for the argumentpublic void addArgument(String name, String value)
addArgument
in class ConnectionRequest
name
- the key of the argumentvalue
- the value for the argumentprotected long calculateContentLength()
protected void buildRequestBody(OutputStream os) throws IOException
buildRequestBody
in class ConnectionRequest
os
- output stream of the bodyIOException
public int getContentLength()
ConnectionRequest
getContentLength
in class ConnectionRequest
public boolean onRedirect(String url)
ConnectionRequest
onRedirect
in class ConnectionRequest
url
- the url to be redirectedpublic boolean isManualRedirect()
public void setManualRedirect(boolean autoRedirect)
autoRedirect
- the autoRedirect to setpublic static void setCanFlushStream(boolean flush)
public boolean isBase64Binaries()
public void setBase64Binaries(boolean base64Binaries)
base64Binaries
- the base64Binaries to set