如何使用Apache HttpClient 4获取文件上传的进度条?
我有以下代码用于使用Apache的HTTP-Client(org.apache.http.client)上传文件:
public static void main(String[] args) throws Exception
{
String fileName = "test.avi";
File file = new File(fileName);
String serverResponse = null;
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
HttpPut put = new HttpPut("http://localhost:8080/" + fileName);
FileEntity fileEntity = new FileEntity(file, "binary/octet-stream");
put.setEntity(fileEntity);
HttpResponse response = client.execute(put);
HttpEntity entity = response.getEntity();
if (entity != null)
{
serverResponse = EntityUtils.toString(entity);
System.out.println(serverResponse);
}
}
它工作得很好,但现在我想有一个进度条,显示文件上传的进度。这是如何做到的?我在File Upload with Java(带进度条)上找到了一个代码片段,但它是为Apache HTTP Client 3(org.apache.commons.httpclient)设计的,RequestEntity类在Apache HTTP Client 4中不存在。;(
也许你们中的某个人有办法?
许多问候
班尼