`
z303729470
  • 浏览: 128929 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用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();
分享到:
评论
1 楼 ljy520zhiyong 2012-06-14  
可不可以发那个http://localhost/upload的action看看?

相关推荐

Global site tag (gtag.js) - Google Analytics