远程访问(post 传参数) 以及IOUtils复制文件

 1 package com.action;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.InputStream;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 import org.apache.commons.io.IOUtils;
10 import org.apache.http.HttpEntity;
11 import org.apache.http.HttpResponse;
12 import org.apache.http.NameValuePair;
13 import org.apache.http.client.HttpClient;
14 import org.apache.http.client.entity.UrlEncodedFormEntity;
15 import org.apache.http.client.methods.HttpPost;
16 import org.apache.http.entity.StringEntity;
17 import org.apache.http.impl.client.DefaultHttpClient;
18 import org.apache.http.message.BasicNameValuePair;
19 
20 public class IOUtilTest {
21 
22     public static void main(String[] args) throws Exception{
23         HttpClient client = new DefaultHttpClient();
24         String URL = "http://www.ola.com.cn/img/icn_joke.png";
25         //    String URL = "http://www.baidu.com/";
26         //    HttpGet get = new HttpGet(URL);
27         HttpPost post = new HttpPost(URL);
28         NameValuePair p = new BasicNameValuePair("v", "123");
29         List<NameValuePair> list = new ArrayList<NameValuePair>();
30         list.add(new BasicNameValuePair("v", "140532246669"));
31         StringEntity entity = new UrlEncodedFormEntity(list);
32         post.setEntity(entity);
33         HttpResponse res = client.execute(post);
34         //if(res.getStatusLine().getStatusCode()==200){
35         //         String result = EntityUtils.toString(res.getEntity());
36         //         System.out.println(result+"
");
37         //}
38         HttpEntity e = res.getEntity();
39         InputStream is = null;
40         is = e.getContent();
41         File file = new File("D:\2.png");
42         FileOutputStream fos = new FileOutputStream(file);
43         IOUtils.copy(is, fos);
44         IOUtils.closeQuietly(is);
45         fos.close();
46     }
47 }
原文地址:https://www.cnblogs.com/Wen-yu-jing/p/3848929.html