我有以下代码用于使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但现在我想要一个进度条,显示文件上传的进度.怎么做到这一点?我在File Upload with Java(带进度条)中找到了一个代码片段,但是它是为Apache HTTP Client 3(org.apache.commons.httpclient)设计的,并且Apache HTTP Client …