问题描述
我尝试使用 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://stackoverflow.com/a/7319110/268795
Main source for this solution: https://stackoverflow.com/a/7319110/268795
这篇关于带有进度的HttpClient Post和MultipartEntityBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!