Example usage for org.apache.http.entity.mime.content InputStreamBody getContentLength

List of usage examples for org.apache.http.entity.mime.content InputStreamBody getContentLength

Introduction

In this page you can find the example usage for org.apache.http.entity.mime.content InputStreamBody getContentLength.

Prototype

public long getContentLength() 

Source Link

Usage

From source file:edu.mit.mobile.android.locast.net.NetworkClient.java

/**
 * Uploads the content using HTTP POST of a multipart MIME document.
 *
 * @param context// ww  w  .  j av a 2  s .co  m
 * @param progressListener
 * @param serverPath
 * @param localFile
 * @param contentType
 * @return
 * @throws NetworkProtocolException
 * @throws IOException
 * @throws JSONException
 * @throws IllegalStateException
 */
public JSONObject uploadContentUsingForm(Context context, TransferProgressListener progressListener,
        String serverPath, Uri localFile, String contentType)
        throws NetworkProtocolException, IOException, IllegalStateException, JSONException {

    if (localFile == null) {
        throw new IOException("Cannot send. Content item does not reference a local file.");
    }

    final InputStream is = getFileStream(context, localFile);

    // next step is to send the file contents.
    final String postUrl = getFullUrlAsString(serverPath);
    final HttpPost r = new HttpPost(postUrl);

    if (DEBUG) {
        Log.d(TAG, "Multipart-MIME POSTing " + localFile + " (mimetype: " + contentType + ") to " + postUrl);
    }

    final InputStreamWatcher isw = new InputStreamWatcher(is, progressListener);

    final MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    final InputStreamBody fileBody = new AndroidFileInputStreamBody(context, localFile, isw, contentType);

    if (DEBUG) {
        Log.d(TAG, "uploadContentUsingForm. content length: " + fileBody.getContentLength());
    }

    reqEntity.addPart("file", fileBody);

    r.setEntity(reqEntity);

    final HttpResponse c = this.execute(r);
    checkStatusCode(c, true);

    return toJsonObject(c);
}