HttpPost文件上传状态进度条

时间:2014-03-05 08:42:08

标签: java http-post httpclient

HttpPost显示文件上传状态,我想进行进度条。我能怎么做。 感谢

public void post(String url, File sendFile) throws UnsupportedEncodingException, IOException {  
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(url);
MultipartEntity multiEntity = new MultipartEntity(); 
multiEntity.addPart("userfile", new FileBody(sendFile));
post.setEntity(multiEntity);  
HttpResponse response = client.execute(post); 
if (response != null) {
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
    if (resEntity != null) {
  System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
  resEntity.consumeContent();
} 
}

1 个答案:

答案 0 :(得分:0)

不确定apache httpclient是否有现成的解决方案,但您可以使用InputStreamBody(而不是FileBody)并将FileInputStream包装在计算已读取的数量的内容中。将其与文件大小进行比较,看看你的距离。