Java – httpclient publishing progress and multipartentitybuilder
•
Java
I tried using httpclient 4.3 3 and multipartytybuilder achieve progress when uploading files
So I actually 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);
Do you know how to get real progress from uploading? I searched a lot - but these examples are for Android, not simple Java or old versions of httpclient, and they don't work for me
Solution
I just found a solution:
You need an httpentitywrapper to calculate the bytes processed and call back
ProgresshttpentityWrapper.ProgressCallback progressCallback = new ProgresshttpentityWrapper.ProgressCallback() { @Override public void progress(float progress) { //Use the progress } } httpPost.setEntity(new ProgresshttpentityWrapper(entityBuilder.build(),progressCallback));
This is progresshttpentitywrapper: https://github.com/x2on/gradle-hockeyapp-plugin/blob/master/src/main/groovy/de/felixschulze/gradle/util/ProgressHttpEntityWrapper.groovy Complete code for
The main sources of this solution are: https://stackoverflow.com/a/7319110/268795
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码