【问题标题】:HttpPost file upload status progress barHttpPost文件上传状态进度条
【发布时间】:2014-04-07 04:54:34
【问题描述】:

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

【问题讨论】:

    标签: java http-post httpclient


    【解决方案1】:

    不确定 apache httpclient 是否有现成的解决方案,但您可以使用 InputStreamBody(而不是 FileBody)并将 FileInputStream 包装在可以计算已读取多少的东西中。将此与文件的大小进行比较,以了解您的进度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      相关资源
      最近更新 更多