public class MultipartRequest

  1. Object
  2. ConnectionRequest
  3. MultipartRequest

ImplementsIOProgressListener

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:

// File: MultipartClientSample.java
MultipartRequest request = new MultipartRequest();
request.setUrl(url);
request.addData("myFileName", fullPathToFile, "text/plain");
NetworkManager.getInstance().addToQueue(request);
// File: UploadServlet.java
@WebServlet(name = "UploadServlet", urlPatterns = {"/upload"})
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 100, // 10 MB
        maxFileSize = 1024 * 1024 * 150, // 50 MB
        maxRequestSize = 1024 * 1024 * 200)      // 100 MB
public class UploadServlet extends HttpServlet {
@Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        Collection parts = req.getParts();
        Part data = parts.iterator().next();
        try(InputStream is = data.getInputStream()) {}
            // store or do something with the input stream
        }
    }
}

The sample code below demonstrates uploading to the filestack.com API.

public void pictureUpload(final Callback resultURL) {
    String picture = Capture.capturePhoto(1024, -1);
    if(picture!=null){
        String filestack = "https://www.filestackapi.com/api/store/S3?key=MY_KEY&filename=myPicture.jpg";
        MultipartRequest request = new MultipartRequest() {
           protected void readResponse(InputStream input) throws IOException  {
              JSONParser jp = new JSONParser();
              Map result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
              String url = (String)result.get("url");
              if(url == null) {
                 resultURL.onError(null, null, 1, result.toString());
                 return;
              }
              resultURL.onSucess(url);
           }
        };
        request.setUrl(filestack);
        try {
            request.addData("fileUpload", picture, "image/jpeg");
            request.setFilename("fileUpload", "myPicture.jpg");
            NetworkManager.getInstance().addToQueue(request);
        } catch(IOException err) {
            err.printStackTrace();
        }
    }
}

Constructors

public MultipartRequest()Initialize variables

Methods

public static boolean isLeaveInputStreamsOpen()Special flag to keep input stream files open after they are read
public static void setLeaveInputStreamsOpen(boolean aLeaveInputStreamsOpen)Special flag to keep input stream files open after they are read
public 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.
public String getBoundary()Returns the boundary string which is normally generated based on system time
public void setBoundary(String boundary)Sets the boundary string, normally you don’t need this method.
protected void initConnection(Object connection)Invoked to initialize HTTP headers, cookies etc.
public void addData(String name, byte[] data, String mimeType)Adds a binary argument to the arguments
public void addData(String name, String filePath, String mimeType) throws IOExceptionAdds a binary argument to the arguments
public 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
public void setFilename(String arg, String filename)Sets the filename for the given argument
public 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
public 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
public 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
public 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
public void addArgument(String name, String value)Add an argument to the request response
protected long calculateContentLength()
protected void buildRequestBody(OutputStream os) throws IOExceptionInvoked when send body is true, by default sends the request arguments based on “POST” conventions
public int getContentLength()Returns the content length header value
public boolean onRedirect(String url)This is a callback method that been called when there is a redirect.
public boolean isManualRedirect()By default redirect responses (302 etc.) are handled manually in multipart requests
public void setManualRedirect(boolean autoRedirect)By default redirect responses (302 etc.) are handled manually in multipart requests, set this to false to handle the redirect.
public boolean isBase64Binaries()Set to true to encode binary data as base 64
public void setBase64Binaries(boolean base64Binaries)Set to true to encode binary data as base 64
public boolean equals(Object o)Indicates whether some other object is “equal to” this one.
public int hashCode()Returns a hash code value for the object.

Inherited nested types

Inherited fields

Inherited methods

From ConnectionRequest

getDefaultCacheMode, setDefaultCacheMode, isReadResponseForErrorsDefault, setReadResponseForErrorsDefault, isHandleErrorCodesInGlobalErrorHandler, setHandleErrorCodesInGlobalErrorHandler, getCookieHeader, setCookieHeader, isCookiesEnabledDefault, setCookiesEnabledDefault, isDefaultFollowRedirects, setDefaultFollowRedirects, isReadTimeoutSupported, purgeCacheDirectory, getDefaultUserAgent, setDefaultUserAgent, setUseNativeCookieStore, isNativeCookieSharingSupported, fetchJSON, fetchJSONAsync, getCacheMode, setCacheMode, isCheckSSLCertificates, setCheckSSLCertificates, isInsecure, setInsecure, getResponseData, getHttpMethod, setHttpMethod, addRequestHeader, removeRequestHeader, removeRequestHeaderIfUnchanged, checkSSLCertificates, getReadTimeout, setReadTimeout, getCachedData, purgeCache, cacheUnmodified, cookieReceived, cookieSent, initCookieHeader, getResponseCode, getResposeCode, shouldConvertPostToGetOnRedirect, readHeaders, readErrorCodeHeaders, getHeader, getHeaders, getHeaderFieldNames, getYield, shouldAutoCloseResponse, handleIOException, handleRuntimeException, handleException, canGetSSLCertificates, getSSLCertificates, handleErrorResponseCode, retry, readResponse, postResponse, createRequestURL, shouldWriteUTFAsGetBytes, kill, shouldStop, isPausable, pause, resume, isPost, setPost, addArgument, removeArgument, removeAllArguments, addArgumentArray, addArguments, getContentType, setContentType, isWriteRequest, setWriteRequest, isReadRequest, setReadRequest, isPaused, setPaused, isKilled, setKilled, getPriority, setPriority, getUserAgent, setUserAgent, isFollowRedirects, setFollowRedirects, getTimeout, setTimeout, ioStreamUpdate, getUrl, setUrl, addResponseListener, removeResponseListener, addResponseCodeListener, addExceptionListener, removeResponseCodeListener, removeExceptionListener, hasResponseListeners, fireResponseListener, isDuplicateSupported, setDuplicateSupported, validate, getDisposeOnCompletion, setDisposeOnCompletion, getShowOnInit, setShowOnInit, getSilentRetryCount, setSilentRetryCount, isFailSilently, setFailSilently, isReadResponseForErrors, setReadResponseForErrors, getResponseContentType, isRedirecting, getDestinationFile, setDestinationFile, getDestinationStorage, setDestinationStorage, isCookiesEnabled, setCookiesEnabled, setChunkedStreamingMode, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, getRequestBody, setRequestBody, setRequestBody, getRequestBodyData, getResponseErrorMessage

Constructor details

MultipartRequest

public MultipartRequest()
Initialize variables

Method details

isLeaveInputStreamsOpen

public static boolean isLeaveInputStreamsOpen()
Special flag to keep input stream files open after they are read

Returns

the leaveInputStreamsOpen

setLeaveInputStreamsOpen

public static void setLeaveInputStreamsOpen(boolean aLeaveInputStreamsOpen)
Special flag to keep input stream files open after they are read

Parameters

aLeaveInputStreamsOpen boolean
the leaveInputStreamsOpen to set

setCanFlushStream

public 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. This method can indicate to the upload to not use the flushing mechanism.

getBoundary

public String getBoundary()
Returns the boundary string which is normally generated based on system time

Returns

the multipart boundary string

setBoundary

public void setBoundary(String boundary)
Sets the boundary string, normally you don’t need this method. Its useful to workaround server issues only. Notice that this method must be invoked before adding any elements.

Parameters

boundary String
the boundary string

initConnection

protected void initConnection(Object connection)
Invoked to initialize HTTP headers, cookies etc.

Parameters

connection Object
the connection object

addData

public void addData(String name, byte[] data, String mimeType)
Adds a binary argument to the arguments

Parameters

name String
the name of the data
data byte[]
the data as bytes
mimeType String
the mime type for the content

addData

public void addData(String name, String filePath, String mimeType) throws IOException
Adds a binary argument to the arguments

Parameters

name String
the name of the file data
filePath String
the path of the file to upload
mimeType String
the mime type for the content

Throws

IOException
if the file cannot be opened

addData

public 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

Parameters

name String
the name of the data
data InputStream
the data stream
dataSize long
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) method
mimeType String
the mime type for the content

setFilename

public void setFilename(String arg, String filename)
Sets the filename for the given argument

Parameters

arg String
the argument name
filename String
the file name

addArgumentNoEncoding

public 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

Parameters

key String
the key of the argument
value String
the value for the argument

addArgumentNoEncoding

public 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

Parameters

key String
the key of the argument
value String[]
the value for the argument

addArgumentNoEncodingArray

public 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

Parameters

key String
the key of the argument
value String...
the value for the argument

addArgument

public 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

Parameters

name String
the key of the argument
value String[]
the value for the argument

addArgument

public void addArgument(String name, String value)
Add an argument to the request response

Parameters

name String
the key of the argument
value String
the value for the argument

calculateContentLength

protected long calculateContentLength()

buildRequestBody

protected void buildRequestBody(OutputStream os) throws IOException
Invoked when send body is true, by default sends the request arguments based on “POST” conventions

Parameters

os OutputStream
output stream of the body

getContentLength

public int getContentLength()
Returns the content length header value

Returns

the content length

onRedirect

public boolean onRedirect(String url)
This is a callback method that been called when there is a redirect. IMPORTANT this feature doesn’t work on all platforms and currently doesn’t work on iOS which always implicitly redirects

Parameters

url String
the url to be redirected

Returns

true if the implementation would like to handle this by itself

isManualRedirect

public boolean isManualRedirect()
By default redirect responses (302 etc.) are handled manually in multipart requests

Returns

the autoRedirect

setManualRedirect

public void setManualRedirect(boolean autoRedirect)
By default redirect responses (302 etc.) are handled manually in multipart requests, set this to false to handle the redirect. Notice that a redirect converts a post to a get.

Parameters

autoRedirect boolean
the autoRedirect to set

isBase64Binaries

public boolean isBase64Binaries()
Set to true to encode binary data as base 64

Returns

the base64Binaries

setBase64Binaries

public void setBase64Binaries(boolean base64Binaries)
Set to true to encode binary data as base 64

Parameters

base64Binaries boolean
the base64Binaries to set

equals

public boolean equals(Object o)
Indicates whether some other object is “equal to” this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

hashCode

public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)