hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar
Date Fri, 04 Oct 2013 08:35:23 GMT
On Thu, 2013-10-03 at 14:50 -0700, Phlip wrote:
> 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...)
> 

You need to decorate the resultant HttpEntity and inside #writeTo method
wrap the original OutputStream with a ProxyOutputStream [1] subclass or
a similar implementation and have it update the UI.

Oleg

[1]
http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/output/ProxyOutputStream.html



---------------------------------------------------------------------
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