带有进度的HttpClient Post和MultipartEntityBuilder [英] HttpClient Post with progress and MultipartEntityBuilder

查看:171
本文介绍了带有进度的HttpClient Post和MultipartEntityBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 HttpClient上传文件时实现进度 4.3.3和 MultipartEntityBuilder

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

知道如何从上传中获得实际进展吗?我搜索了很多 - 但是示例是针对android而不是普通的java或旧版本的 HttpClient 并且对我不起作用...

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

推荐答案

我刚刚找到了解决方案:

I just found a solution for that:

你需要 HttpEntityWrapper 进行计数的处理的字节,并具有一个回调。

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

这是来自 ProgressHttpEntityWrapper : https://github.com/x2on/gradle-hockeyapp-plugin/blob/master/src/main/groovy/de/felixschulze/gradle/util/ProgressHttpEntityWrapper.groovy

此解决方案的主要来源: https://stackoverflow.com/a/7319110/268795

Main source for this solution: https://stackoverflow.com/a/7319110/268795

这篇关于带有进度的HttpClient Post和MultipartEntityBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆