HttpClient Post with progress and MultipartEntityBuilder
Asked Answered
C

1

7

I try to implement an progress while uploading an file with HttpClient 4.3.3 and MultipartEntityBuilder

So actually i execute a post request with the following code

HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.build();

HttpPost httpPost = new HttpPost(uploadUrl);

MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addPart("filea", new FileBody(filea));
entityBuilder.addPart("fileb", new FileBody(fileb));

final HttpEntity entity = entityBuilder.build();
httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);

Any idea how to get the actual progress from the upload? I searched a lot - but the examples are for android and not plain java or an older version of HttpClient and didn't work for me...

Clubfoot answered 8/4, 2014 at 9:24 Comment(1)
Hi Please refer this link Its solve my problem. <#22933321>Tours
C
17

I just found a solution for that:

You need an HttpEntityWrapper which counts the processed bytes and has a callback.

ProgressHttpEntityWrapper.ProgressCallback progressCallback = new ProgressHttpEntityWrapper.ProgressCallback() {

        @Override
        public void progress(float progress) {
            //Use the progress
        }

    }

httpPost.setEntity(new ProgressHttpEntityWrapper(entityBuilder.build(), progressCallback));

And here is the complete code from the ProgressHttpEntityWrapper: https://github.com/x2on/gradle-hockeyapp-plugin/blob/master/src/main/groovy/de/felixschulze/gradle/util/ProgressHttpEntityWrapper.groovy

Main source for this solution: https://mcmap.net/q/245218/-file-upload-with-java-with-progress-bar

Clubfoot answered 8/4, 2014 at 16:6 Comment(2)
Hi, I have used your solution for calculating uploaded file size and remaining file size but it differ from actual file size present in gallery. Do you have any idea? why the file size gets vary?Tours
There is a delay between ProgressCallback reporting 100% creation and the HttpServer actually responding status ok, what accounts for the diferenceDeviationism

© 2022 - 2024 — McMap. All rights reserved.