zoukankan      html  css  js  c++  java
  • 远程访问(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 }
  • 相关阅读:
    谈谈对MapTask任务分配和Shuffle的理解
    Yarn的资源调优
    @section Scripts{}的使用
    数据提高查询速度的方法(摘抄)
    customErrors 元素(ASP.NET 设置架构)
    成员资格、授权和安全性(一)
    MVC5发展历程,从MVC2谈起
    轻量级记事本工具:CintaNotes
    CF459E Pashmak and Graph [dp]
    【模板】拉格朗日插值
  • 原文地址:https://www.cnblogs.com/Wen-yu-jing/p/3848929.html
Copyright © 2011-2022 走看看