2014-03-05 133 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。將此與文件的大小進行比較,以瞭解您有多遠。