|  | @ -1,408 +0,0 @@
 | 
	
		
			
				|  |  | package com.yihu.ehr.util.http;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import org.apache.http.Consts;
 | 
	
		
			
				|  |  | import org.apache.http.HttpEntity;
 | 
	
		
			
				|  |  | import org.apache.http.NameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.client.CredentialsProvider;
 | 
	
		
			
				|  |  | import org.apache.http.client.config.RequestConfig;
 | 
	
		
			
				|  |  | import org.apache.http.client.entity.UrlEncodedFormEntity;
 | 
	
		
			
				|  |  | import org.apache.http.client.methods.*;
 | 
	
		
			
				|  |  | import org.apache.http.client.protocol.HttpClientContext;
 | 
	
		
			
				|  |  | import org.apache.http.client.utils.URLEncodedUtils;
 | 
	
		
			
				|  |  | import org.apache.http.entity.mime.MultipartEntityBuilder;
 | 
	
		
			
				|  |  | import org.apache.http.entity.mime.content.FileBody;
 | 
	
		
			
				|  |  | import org.apache.http.impl.client.BasicCredentialsProvider;
 | 
	
		
			
				|  |  | import org.apache.http.impl.client.CloseableHttpClient;
 | 
	
		
			
				|  |  | import org.apache.http.impl.client.HttpClients;
 | 
	
		
			
				|  |  | import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.util.EntityUtils;
 | 
	
		
			
				|  |  | import org.springframework.util.StringUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.File;
 | 
	
		
			
				|  |  | import java.io.FileOutputStream;
 | 
	
		
			
				|  |  | import java.io.IOException;
 | 
	
		
			
				|  |  | import java.io.InputStream;
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * HTTP 请求工具类
 | 
	
		
			
				|  |  |  *
 | 
	
		
			
				|  |  |  * @author : cwd
 | 
	
		
			
				|  |  |  * @version : 1.0.0
 | 
	
		
			
				|  |  |  * @date : 2016/1/14
 | 
	
		
			
				|  |  |  * @see : TODO
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | public class HttpClientUtil {
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送 GET 请求(HTTP),不带输入数据 不加密
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doGet(String url) throws Exception {
 | 
	
		
			
				|  |  |         return doGet(url, new HashMap<String, Object>(), "", "");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送 GET 请求(HTTP),不带输入数据 不加密
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doGet(String url, Map<String, Object> params) throws Exception {
 | 
	
		
			
				|  |  |         return doGet(url, params, "", "");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送 GET 请求(HTTP),不带输入数据 加密
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doGet(String url, String username, String password) throws Exception {
 | 
	
		
			
				|  |  |         return doGet(url, new HashMap<String, Object>(), username, password);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * httpClient的get请求方式
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doGet(String url, Map<String, Object> params, String username, String password)
 | 
	
		
			
				|  |  |             throws Exception {
 | 
	
		
			
				|  |  |         String responString = "";
 | 
	
		
			
				|  |  |         CloseableHttpResponse response1 = null;
 | 
	
		
			
				|  |  |         List<BasicNameValuePair> jsonParams = new ArrayList<>();
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  |         //设置请求信息
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             RequestConfig requestConfig = RequestConfig.custom().
 | 
	
		
			
				|  |  |                     setAuthenticationEnabled(true).build();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //配置参数
 | 
	
		
			
				|  |  |             for (String key : params.keySet()) {
 | 
	
		
			
				|  |  |                 jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             HttpGet httpget = new HttpGet(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
 | 
	
		
			
				|  |  |             httpget.setConfig(requestConfig);
 | 
	
		
			
				|  |  |             if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
 | 
	
		
			
				|  |  |                 //需要验证
 | 
	
		
			
				|  |  |                 HttpClientContext context = HttpClientContext.create();
 | 
	
		
			
				|  |  |                 //通过http的上下文设置账号密码
 | 
	
		
			
				|  |  |                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 | 
	
		
			
				|  |  |                 credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
 | 
	
		
			
				|  |  |                         new org.apache.http.auth.UsernamePasswordCredentials(username, password));
 | 
	
		
			
				|  |  |                 context.setCredentialsProvider(credsProvider);
 | 
	
		
			
				|  |  |                 response1 = httpclient.execute(httpget, context);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 response1 = httpclient.execute(httpget);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpEntity entity1 = response1.getEntity();
 | 
	
		
			
				|  |  |             responString = EntityUtils.toString(entity1, "UTF-8");
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             response1.close();
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return responString;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * httpClient的post请求方式
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param params
 | 
	
		
			
				|  |  |      * @param username
 | 
	
		
			
				|  |  |      * @param password
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doPost(String url, Map<String, Object> params, String username, String password) throws Exception {
 | 
	
		
			
				|  |  |         String responString = "";
 | 
	
		
			
				|  |  |         CloseableHttpResponse response = null;
 | 
	
		
			
				|  |  |         List<BasicNameValuePair> jsonParams = new ArrayList<>();
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             HttpPost httpPost = new HttpPost(url);
 | 
	
		
			
				|  |  |             //设置请求信息
 | 
	
		
			
				|  |  |             RequestConfig requestConfig = RequestConfig.custom().
 | 
	
		
			
				|  |  |                     setAuthenticationEnabled(true).build();
 | 
	
		
			
				|  |  |             //设置参数
 | 
	
		
			
				|  |  |             List<NameValuePair> formparams = new ArrayList<NameValuePair>();
 | 
	
		
			
				|  |  |             for (Map.Entry<String, Object> entry : params.entrySet()) {
 | 
	
		
			
				|  |  |                 formparams.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             httpPost.setEntity(entity);
 | 
	
		
			
				|  |  |             httpPost.setConfig(requestConfig);
 | 
	
		
			
				|  |  |             if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
 | 
	
		
			
				|  |  |                 //需要验证
 | 
	
		
			
				|  |  |                 HttpClientContext context = HttpClientContext.create();
 | 
	
		
			
				|  |  |                 //通过http的上下文设置账号密码
 | 
	
		
			
				|  |  |                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 | 
	
		
			
				|  |  |                 credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
 | 
	
		
			
				|  |  |                         new org.apache.http.auth.UsernamePasswordCredentials(username, password));
 | 
	
		
			
				|  |  |                 context.setCredentialsProvider(credsProvider);
 | 
	
		
			
				|  |  |                 response = httpclient.execute(httpPost, context);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 response = httpclient.execute(httpPost);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpEntity httpEntity = response.getEntity();
 | 
	
		
			
				|  |  |             //流转字符串
 | 
	
		
			
				|  |  |             responString = EntityUtils.toString(httpEntity);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             response.close();
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return responString;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * httpClient的put请求方式
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param params
 | 
	
		
			
				|  |  |      * @param username
 | 
	
		
			
				|  |  |      * @param password
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doPut(String url, Map<String, Object> params, String username, String password) throws Exception {
 | 
	
		
			
				|  |  |         String responString = "";
 | 
	
		
			
				|  |  |         CloseableHttpResponse response1 = null;
 | 
	
		
			
				|  |  |         List<BasicNameValuePair> jsonParams = new ArrayList<>();
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             //设置请求信息
 | 
	
		
			
				|  |  |             RequestConfig requestConfig = RequestConfig.custom().
 | 
	
		
			
				|  |  |                     setAuthenticationEnabled(true).build();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //配置参数
 | 
	
		
			
				|  |  |             for (String key : params.keySet()) {
 | 
	
		
			
				|  |  |                 jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpPut httpPut = new HttpPut(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
 | 
	
		
			
				|  |  |             httpPut.setConfig(requestConfig);
 | 
	
		
			
				|  |  |             if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
 | 
	
		
			
				|  |  |                 //需要验证
 | 
	
		
			
				|  |  |                 HttpClientContext context = HttpClientContext.create();
 | 
	
		
			
				|  |  |                 //通过http的上下文设置账号密码
 | 
	
		
			
				|  |  |                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 | 
	
		
			
				|  |  |                 credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
 | 
	
		
			
				|  |  |                         new org.apache.http.auth.UsernamePasswordCredentials(username, password));
 | 
	
		
			
				|  |  |                 context.setCredentialsProvider(credsProvider);
 | 
	
		
			
				|  |  |                 response1 = httpclient.execute(httpPut, context);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 response1 = httpclient.execute(httpPut);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpEntity entity1 = response1.getEntity();
 | 
	
		
			
				|  |  |             responString = EntityUtils.toString(entity1, "UTF-8");
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             response1.close();
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return responString;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * httpClient的delete请求方式
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param params
 | 
	
		
			
				|  |  |      * @param username
 | 
	
		
			
				|  |  |      * @param password
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String doDelete(String url, Map<String, Object> params, String username, String password) throws Exception {
 | 
	
		
			
				|  |  |         String responString = "";
 | 
	
		
			
				|  |  |         CloseableHttpResponse response1 = null;
 | 
	
		
			
				|  |  |         List<BasicNameValuePair> jsonParams = new ArrayList<>();
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             //设置请求信息
 | 
	
		
			
				|  |  |             RequestConfig requestConfig = RequestConfig.custom().
 | 
	
		
			
				|  |  |                     setAuthenticationEnabled(true).build();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //配置参数
 | 
	
		
			
				|  |  |             for (String key : params.keySet()) {
 | 
	
		
			
				|  |  |                 jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpDelete httpget = new HttpDelete(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
 | 
	
		
			
				|  |  |             httpget.setConfig(requestConfig);
 | 
	
		
			
				|  |  |             if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
 | 
	
		
			
				|  |  |                 //需要验证
 | 
	
		
			
				|  |  |                 HttpClientContext context = HttpClientContext.create();
 | 
	
		
			
				|  |  |                 //通过http的上下文设置账号密码
 | 
	
		
			
				|  |  |                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 | 
	
		
			
				|  |  |                 credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
 | 
	
		
			
				|  |  |                         new org.apache.http.auth.UsernamePasswordCredentials(username, password));
 | 
	
		
			
				|  |  |                 context.setCredentialsProvider(credsProvider);
 | 
	
		
			
				|  |  |                 response1 = httpclient.execute(httpget, context);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 response1 = httpclient.execute(httpget);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpEntity entity1 = response1.getEntity();
 | 
	
		
			
				|  |  |             responString = EntityUtils.toString(entity1, "UTF-8");
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             response1.close();
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return responString;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 文件上传
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param file
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param username
 | 
	
		
			
				|  |  |      * @param password
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws IOException
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String sendFile(File file, String url, String username, String password) throws IOException {
 | 
	
		
			
				|  |  |         String responString = "";
 | 
	
		
			
				|  |  |         CloseableHttpResponse response = null;
 | 
	
		
			
				|  |  |         List<BasicNameValuePair> jsonParams = new ArrayList<>();
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             HttpPost httpPost = new HttpPost(url);
 | 
	
		
			
				|  |  |             //设置请求信息
 | 
	
		
			
				|  |  |             RequestConfig requestConfig = RequestConfig.custom().
 | 
	
		
			
				|  |  |                     setAuthenticationEnabled(true).build();
 | 
	
		
			
				|  |  |             httpPost.setConfig(requestConfig);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             FileBody bin = new FileBody(file);
 | 
	
		
			
				|  |  |             MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
 | 
	
		
			
				|  |  |             reqEntity.addPart("file", bin);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             httpPost.setEntity(reqEntity.build());
 | 
	
		
			
				|  |  |             if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
 | 
	
		
			
				|  |  |                 //需要验证
 | 
	
		
			
				|  |  |                 HttpClientContext context = HttpClientContext.create();
 | 
	
		
			
				|  |  |                 //通过http的上下文设置账号密码
 | 
	
		
			
				|  |  |                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 | 
	
		
			
				|  |  |                 credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
 | 
	
		
			
				|  |  |                         new org.apache.http.auth.UsernamePasswordCredentials(username, password));
 | 
	
		
			
				|  |  |                 context.setCredentialsProvider(credsProvider);
 | 
	
		
			
				|  |  |                 response = httpclient.execute(httpPost, context);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 response = httpclient.execute(httpPost);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpEntity httpEntity = response.getEntity();
 | 
	
		
			
				|  |  |             //流转字符串
 | 
	
		
			
				|  |  |             responString = EntityUtils.toString(httpEntity);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             response.close();
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return responString;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 文件下载
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param filePath
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param username
 | 
	
		
			
				|  |  |      * @param password
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static File downFile(String filePath, Map<String, Object> params, String url, String username, String password) throws Exception {
 | 
	
		
			
				|  |  |         File file = null;
 | 
	
		
			
				|  |  |         CloseableHttpResponse response = null;
 | 
	
		
			
				|  |  |         List<BasicNameValuePair> jsonParams = new ArrayList<>();
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             HttpPost httpPost = new HttpPost(url);
 | 
	
		
			
				|  |  |             //设置请求信息
 | 
	
		
			
				|  |  |             RequestConfig requestConfig = RequestConfig.custom().
 | 
	
		
			
				|  |  |                     setAuthenticationEnabled(true).build();
 | 
	
		
			
				|  |  |             //设置参数
 | 
	
		
			
				|  |  |             List<NameValuePair> formparams = new ArrayList<NameValuePair>();
 | 
	
		
			
				|  |  |             for (Map.Entry<String, Object> entry : params.entrySet()) {
 | 
	
		
			
				|  |  |                 formparams.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
 | 
	
		
			
				|  |  |             httpPost.setEntity(entity);
 | 
	
		
			
				|  |  |             httpPost.setConfig(requestConfig);
 | 
	
		
			
				|  |  |             if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
 | 
	
		
			
				|  |  |                 //需要验证
 | 
	
		
			
				|  |  |                 HttpClientContext context = HttpClientContext.create();
 | 
	
		
			
				|  |  |                 //通过http的上下文设置账号密码
 | 
	
		
			
				|  |  |                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 | 
	
		
			
				|  |  |                 credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
 | 
	
		
			
				|  |  |                         new org.apache.http.auth.UsernamePasswordCredentials(username, password));
 | 
	
		
			
				|  |  |                 context.setCredentialsProvider(credsProvider);
 | 
	
		
			
				|  |  |                 response = httpclient.execute(httpPost, context);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 response = httpclient.execute(httpPost);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             HttpEntity httpEntity = response.getEntity();
 | 
	
		
			
				|  |  |             InputStream is = httpEntity.getContent();
 | 
	
		
			
				|  |  |             file = new File(filePath);
 | 
	
		
			
				|  |  |             file.getParentFile().mkdirs();
 | 
	
		
			
				|  |  |             FileOutputStream fileout = new FileOutputStream(file);
 | 
	
		
			
				|  |  |             /**
 | 
	
		
			
				|  |  |              * 根据实际运行效果 设置缓冲区大小
 | 
	
		
			
				|  |  |              */
 | 
	
		
			
				|  |  |             byte[] buffer = new byte[1024];
 | 
	
		
			
				|  |  |             int ch = 0;
 | 
	
		
			
				|  |  |             while ((ch = is.read(buffer)) != -1) {
 | 
	
		
			
				|  |  |                 fileout.write(buffer, 0, ch);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             is.close();
 | 
	
		
			
				|  |  |             fileout.flush();
 | 
	
		
			
				|  |  |             fileout.close();
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             response.close();
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return file;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static void main(String[] args) throws Exception {
 | 
	
		
			
				|  |  |         Map<String, Object> params = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | //        params.put("standard", "{\n" +
 | 
	
		
			
				|  |  | //                "    \"id\": 4,\n" +
 | 
	
		
			
				|  |  | //                "    \"name\": \"电子健康档案标准bbb\",\n" +
 | 
	
		
			
				|  |  | //                "    \"code\": \"YY20160114\",\n" +
 | 
	
		
			
				|  |  | //                "    \"publisher\": \"健康之路2\",\n" +
 | 
	
		
			
				|  |  | //                "    \"publisherOrgCode\": \"jkzl\",\n" +
 | 
	
		
			
				|  |  | //                "    \"summary\": \"string\",\n" +
 | 
	
		
			
				|  |  | //                "    \"refStandard\": \"WS2012\",\n" +
 | 
	
		
			
				|  |  | //                "    \"refStandardVersion\": \"V1.0\",\n" +
 | 
	
		
			
				|  |  | //                "    \"versionStatus\": 0\n" +
 | 
	
		
			
				|  |  | //                "}");
 | 
	
		
			
				|  |  |         // params.put("versionId", "5694696eb80d");
 | 
	
		
			
				|  |  |         //  params.put("publisher", "lfq");
 | 
	
		
			
				|  |  |         //   System.out.println(HttpClientUtil.doPost("http://192.168.131.17:6020/api/v1.0/standard_center/version/publish", params, "user", "standard"));
 | 
	
		
			
				|  |  |         //文件上传
 | 
	
		
			
				|  |  |         //File file = new File("F:/111.txt");
 | 
	
		
			
				|  |  |         // HttpClientUtil.sendFile(file, "http://192.168.131.6:8890/api/v1.0/resource/upload", "user", "standard");
 | 
	
		
			
				|  |  |         //文件下载
 | 
	
		
			
				|  |  |         params.put("field", "[{\"key\":\"name\",\"value\":\"资源名称\"},{\"key\":\"code\",\"value\":\"资源代码\"},{\"key\":\"type\",\"value\":\"资源类型\"},{\"key\":\"category\",\"value\":\"资源类别\"}]");
 | 
	
		
			
				|  |  |         params.put("name", "授权资源清单");
 | 
	
		
			
				|  |  |         params.put("id", "aaaaaaaa1");
 | 
	
		
			
				|  |  |         params.put("queryParams", "[]");
 | 
	
		
			
				|  |  |         HttpClientUtil.downFile("E:/aaa.xls", params, "http://192.168.131.6:8080/resource/exportAllExcel", "", "");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |