使用InputStreamEntity 边读取边上传文件

HttpClient httpclient = new DefaultHttpClient();
 

HttpPost httppost = new HttpPost("http://localhost/upload");
 

File file = new File("/path/to/myfile");
 
FileInputStream fileInputStream = new FileInputStream(file);
 
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length());
 

httppost.setEntity(reqEntity);
 
reqEntity.setContentType("binary/octet-stream");
 
HttpResponse response = httpclient.execute(httppost);
 
HttpEntity responseEntity = response.getEntity();
 

if (responseEntity != null) {
 
  responseEntity.consumeContent();
 
}
 

httpclient.getConnectionManager().shutdown();

相关推荐