2014-03-05 139 views
0

HttpPost显示文件上传状态,我想制作进度条。我能怎么做。 感谢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(); 
} 
} 

回答

0

不知道如果Apache的HttpClient有这个现成的解决方案,但你可以使用一个InputStreamBody(而不是FileBody),并在一些计算已经被读了多少包裹的FileInputStream。将此与文件的大小进行比较,以了解您有多远。