httpclient 上传、下载文件

转自:http://www.oschina.net/code/snippet_216580_38020

基于HttpClient 4.3.3 的一个上传、下载文件的例子,特转载以供大家学习参考。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 上传文件  
  3.  * @throws ParseException 
  4.  * @throws IOException 
  5.  */     
  6. public static void postFile() throws ParseException, IOException{  
  7.     CloseableHttpClient httpClient = HttpClients.createDefault();  
  8.     try {  
  9.         // 要上传的文件的路径  
  10.         String filePath = new String("F:/pic/001.jpg");  
  11.         // 把一个普通参数和文件上传给下面这个地址 是一个servlet  
  12.         HttpPost httpPost = new HttpPost(  
  13.                 "http://localhost:8080/xxx/xxx.action");  
  14.         // 把文件转换成流对象FileBody  
  15.         File file = new File(filePath);  
  16.         FileBody bin = new FileBody(file);    
  17.         StringBody uploadFileName = new StringBody(  
  18.                 "把我修改成文件名称", ContentType.create(  
  19.                         "text/plain", Consts.UTF_8));  
  20.         //以浏览器兼容模式运行,防止文件名乱码。    
  21.            HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)  
  22.                 .addPart("uploadFile", bin) //uploadFile对应服务端类的同名属性<File类型>  
  23.                 .addPart("uploadFileName", uploadFileName)//uploadFileName对应服务端类的同名属性<String类型>  
  24.                 .setCharset(CharsetUtils.get("UTF-8")).build();  
  25.    
  26.         httpPost.setEntity(reqEntity);  
  27.    
  28.         System.out.println("发起请求的页面地址 " + httpPost.getRequestLine());  
  29.         // 发起请求 并返回请求的响应  
  30.         CloseableHttpResponse response = httpClient.execute(httpPost);  
  31.         try {  
  32.             System.out.println("----------------------------------------");  
  33.             // 打印响应状态  
  34.             System.out.println(response.getStatusLine());  
  35.             // 获取响应对象  
  36.             HttpEntity resEntity = response.getEntity();  
  37.             if (resEntity != null) {  
  38.                 // 打印响应长度  
  39.                 System.out.println("Response content length: "  
  40.                         + resEntity.getContentLength());  
  41.                 // 打印响应内容  
  42.                 System.out.println(EntityUtils.toString(resEntity,  
  43.                         Charset.forName("UTF-8")));  
  44.             }  
  45.             // 销毁  
  46.             EntityUtils.consume(resEntity);  
  47.         } finally {  
  48.             response.close();  
  49.         }  
  50.     } finally {  
  51.         httpClient.close();  
  52.     }  
  53. }  
  54.    
  55.  /** 
  56.  * 下载文件 
  57.  * @param url            http://www.xxx.com/img/333.jpg 
  58.  * @param destFileName   xxx.jpg/xxx.png/xxx.txt 
  59.  * @throws ClientProtocolException 
  60.  * @throws IOException 
  61.  */  
  62. public static void getFile(String url, String destFileName)  
  63.         throws ClientProtocolException, IOException {  
  64.     // 生成一个httpclient对象  
  65.     CloseableHttpClient httpclient = HttpClients.createDefault();  
  66.     HttpGet httpget = new HttpGet(url);  
  67.     HttpResponse response = httpclient.execute(httpget);  
  68.     HttpEntity entity = response.getEntity();  
  69.     InputStream in = entity.getContent();  
  70.     File file = new File(destFileName);  
  71.     try {  
  72.         FileOutputStream fout = new FileOutputStream(file);  
  73.         int l = -1;  
  74.         byte[] tmp = new byte[1024];  
  75.         while ((l = in.read(tmp)) != -1) {  
  76.             fout.write(tmp, 0, l);  
  77.             // 注意这里如果用OutputStream.write(buff)的话,图片会失真,大家可以试试  
  78.         }  
  79.         fout.flush();  
  80.         fout.close();  
  81.     } finally {  
  82.         // 关闭低层流。  
  83.         in.close();  
  84.     }  
  85.     httpclient.close();  
  86. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值