hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phlip <phlip2...@gmail.com>
Subject Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar
Date Thu, 03 Oct 2013 21:50:36 GMT
Here's an example of MultipartEntityBuilder in action:

public static void postFile(String fileName) throws Exception {
    // Based on:
http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(SERVER + "uploadFile");
    MultipartEntityBuilder multipartEntity =
MultipartEntityBuilder.create();
    multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    multipartEntity.addPart("file", new FileBody(new File(fileName)));
    post.setEntity(multipartEntity.build());
    HttpResponse response = client.execute(post);
    HttpEntity entity = response.getEntity();

    // response.getStatusLine();  // CONSIDER  Detect server complaints

    entity.consumeContent();
    client.getConnectionManager().shutdown();

}  // FIXME  Hook up a progress bar!

How do I add a progress bar? Should I extend the FileBody class,
override its getInputStream(), and return an input stream with an
overridden .write that counts the bytes & sends View commands. (I'm
inside Android, if that helps...)

-- 
  Phlip
  http://c2.com/cgi/wiki?ZeekLand

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message