HttpClientUtils

package utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
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;

/**
 * @author say
 * @Description: 基于HttpClient 文件下载 POST GET
 * 
 */
public class HttpClientUtils {

	/**
	 * 最大线程池
	 */
	public static final int THREAD_POOL_SIZE = 20;
	
	private static String charSet = "UTF-8";

	public interface HttpClientDownLoadProgress {
		public void onProgress(int progress);
	}

	// 单例模式创建对象
	private static HttpClientUtils httpClientDownload;

	private ExecutorService downloadExcutorService;

	private HttpClientUtils() {
		downloadExcutorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
	}

	public static HttpClientUtils getInstance() {
		if (httpClientDownload == null) {
			httpClientDownload = new HttpClientUtils();
		}
		return httpClientDownload;
	}

	public static HttpClientUtils getInstance(String charCode) {
		if (httpClientDownload == null) {
			httpClientDownload = new HttpClientUtils();
		}
		// 设置编码
		charSet = charCode;
		return httpClientDownload;
	}

	/**
	 * get请求
	 * 
	 * @param url
	 * @return
	 */
	public String httpGet(String url) {
		return httpGet(url, null);
	}

	/**
	 * http get请求
	 * 
	 * @param url
	 *            请求的url
	 * @return
	 */
	public String httpGet(String url, Map<String, String> headMap) {
		String responseContent = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpGet httpGet = new HttpGet(url);

			// 设置超时时间
			RequestConfig requestConfig = 
					RequestConfig
					.custom()
					.setConnectTimeout(50000)
					.setConnectionRequestTimeout(10000)
					.setSocketTimeout(50000)
//					.setProxy(new HttpHost("203.142.73.234", 8080))
					.build();
			httpGet.setConfig(requestConfig);

			// 放在execute执行之前
			setGetHead(httpGet, headMap);
			CloseableHttpResponse response = httpclient.execute(httpGet);
			try {
//				System.out.println(response.getStatusLine());
				int statusCode = response.getStatusLine().getStatusCode();
				if (200 == statusCode) {
					HttpEntity entity = response.getEntity();
					responseContent = getRespString(entity);
					// System.out.println("debug:" + responseContent);
					EntityUtils.consume(entity);
					return responseContent;
				}
				if (302 == statusCode || 301 == statusCode) {
					String location = null;
					// 获取location的值
					Header[] allHeaders = response.getAllHeaders();
					for (Header header : allHeaders) {
						if ("location".equals(header.getName().toLowerCase())) {
							location = header.getValue();
							break;
						}
					}
					// 重新发送请求
					if (null != location) {
						String result = httpGet(location, headMap);
						return result;
					}
				}
			} finally {
				response.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return responseContent;
	}

	public String httpPost(String url, Map<String, String> paramsMap) {
		return httpPost(url, paramsMap, null);
	}

	/**
	 * http的post请求
	 * 
	 * @param url
	 * @param paramsMap
	 * @return
	 */
	public String httpPost(String url, Map<String, String> paramsMap, Map<String, String> headMap) {
		String responseContent = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httpPost = new HttpPost(url);

			// 设置超时时间
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(50000)
					.setConnectionRequestTimeout(10000).setSocketTimeout(50000).build();
			httpPost.setConfig(requestConfig);

			setPostHead(httpPost, headMap);
			setPostParams(httpPost, paramsMap);
			CloseableHttpResponse response = httpclient.execute(httpPost);
			try {
				if (200 == response.getStatusLine().getStatusCode()) {
					HttpEntity entity = response.getEntity();
					responseContent = getRespString(entity);
					EntityUtils.consume(entity);
				} else {
					System.out.println(response.getStatusLine());
				}
			} finally {
				response.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// System.out.println("responseContent = " + responseContent);
		return responseContent;
	}

	/**
	 * http的post请求, 有302重定向请求
	 * 
	 * @param url
	 * @param paramsMap
	 * @return
	 */
	public String httpPostForRedirect(String url, Map<String, String> paramsMap, Map<String, String> headMap) {
		String responseContent = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httpPost = new HttpPost(url);

			// 设置超时时间
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(50000)
					.setConnectionRequestTimeout(10000).setSocketTimeout(50000).build();
			httpPost.setConfig(requestConfig);

			setPostHead(httpPost, headMap);
			setPostParams(httpPost, paramsMap);
			CloseableHttpResponse response = httpclient.execute(httpPost);
			try {
//				System.out.println(response.getStatusLine());
				int statusCode = response.getStatusLine().getStatusCode();
				if (302 == statusCode || 301 == statusCode) {
					String location = null;
					// 获取location的值
					Header[] allHeaders = response.getAllHeaders();
					for (Header header : allHeaders) {
						if ("location".equals(header.getName().toLowerCase())) {
							location = header.getValue();
							break;
						}
					}
					// 重新发送请求
					if (null != location) {
						String result = httpPost(location, paramsMap);
						return result;
					}
				}

				HttpEntity entity = response.getEntity();
				responseContent = getRespString(entity);
				EntityUtils.consume(entity);
			} finally {
				response.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// System.out.println("responseContent = " + responseContent);
		return responseContent;
	}

	/**
	 * 下载文件,带回调
	 * 
	 * @param url
	 *            图片访问的url
	 * @param filePath
	 *            文件保存的路径 如:D:/test.png
	 */
	public void downloadCallback(final String url, final String filePath) {
		downloadExcutorService.execute(new Runnable() {
			@Override
			public void run() {
				httpDownloadFile(url, filePath, null, null);
			}
		});
	}

	/**
	 * 下载文件
	 * 
	 * @param url
	 *            图片访问的url
	 * @param filePath
	 *            文件保存的路径 如:D:/test.png
	 */
	public void download(final String url, final String filePath) {
		httpDownloadFile(url, filePath, null, null);
	}

	/**
	 * 下载文件
	 * 
	 * @param url
	 * @param filePath
	 * @param progress
	 *            进度回调
	 */
	public void download(final String url, final String filePath, final HttpClientDownLoadProgress progress) {
		downloadExcutorService.execute(new Runnable() {
			@Override
			public void run() {
				httpDownloadFile(url, filePath, progress, null);
			}
		});
	}

	/**
	 * 下载文件
	 * 
	 * @param url
	 * @param filePath
	 */
	private void httpDownloadFile(String url, String filePath, HttpClientDownLoadProgress progress,
			Map<String, String> headMap) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpGet httpGet = new HttpGet(url);
			// 设置超时时间
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(50000)
					.setConnectionRequestTimeout(10000).setSocketTimeout(50000).build();
			httpGet.setConfig(requestConfig);

			setGetHead(httpGet, headMap);
			CloseableHttpResponse response1 = httpclient.execute(httpGet);
			try {
//				System.out.println(response1.getStatusLine());
				HttpEntity httpEntity = response1.getEntity();
				long contentLength = httpEntity.getContentLength();
				InputStream is = httpEntity.getContent();
				// 根据InputStream 下载文件
				ByteArrayOutputStream output = new ByteArrayOutputStream();
				byte[] buffer = new byte[4096];
				int r = 0;
				long totalRead = 0;
				while ((r = is.read(buffer)) > 0) {
					output.write(buffer, 0, r);
					totalRead += r;
					if (progress != null) {// 回调进度
						progress.onProgress((int) (totalRead * 100 / contentLength));
					}
				}
				FileOutputStream fos = new FileOutputStream(filePath);
				output.writeTo(fos);
				output.flush();
				output.close();
				fos.close();
				EntityUtils.consume(httpEntity);
			} finally {
				response1.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 设置POST的参数
	 * 
	 * @param httpPost
	 * @param paramsMap
	 * @throws Exception
	 */
	private void setPostParams(HttpPost httpPost, Map<String, String> paramsMap) throws Exception {
		if (paramsMap != null && paramsMap.size() > 0) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Set<String> keySet = paramsMap.keySet();
			for (String key : keySet) {
				nvps.add(new BasicNameValuePair(key, paramsMap.get(key)));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nvps));
		}
	}

	/**
	 * 设置http的HEAD
	 * 
	 * @param httpPost
	 * @param headMap
	 */
	private void setPostHead(HttpPost httpPost, Map<String, String> headMap) {
		if (headMap != null && headMap.size() > 0) {
			Set<String> keySet = headMap.keySet();
			for (String key : keySet) {
				httpPost.addHeader(key, headMap.get(key));
			}
		} else {
			// 设置默认的请求参数
			httpPost.setHeader("user-agent",	getUserAgent());
			// ...
			httpPost.setHeader(":authority", "item.jd.com");
			httpPost.setHeader(":method", "POST");
			httpPost.setHeader(":scheme", "https");
			httpPost.setHeader("User-Agent",	getUserAgent());
			httpPost.setHeader("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
			httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
			httpPost.setHeader("Cache-Control", "max-age=0");
			httpPost.setHeader("Cookie",
					"ipLoc-djd=1-72-2799-0; mt_xid=V2_52007VwMXUlteVFMaSx5sAjUFQVNfXlpGGxtKXhliVhYHQVBbCBdVS1oBMwQRWltaB1MYeRpdBWEfElFBWVtLH00SXANsAhBiX2hSah9NGFgBYAUXVW1YV1wY; user-key=b9de3f4b-e58f-4091-9803-1dec084c7108; cn=0; unpl=V2_ZzNtbRFSQRZ2ARRRchAJBmIDEg8RUUERJwtBB30eVFBgChZdclRCFXMURldnGV0UZwQZWUdcQRdFCHZXchBYAWcCGllyBBNNIEwHDCRSBUE3XHxcFVUWF3RaTwEoSVoAYwtBDkZUFBYhW0IAKElVVTUFR21yVEMldQl2VH4ZWgNhChdfRWdzEkU4dlN6H1sAZDMTbUNnAUEpAEJcfR9fSGcGEltEUUoQdw92VUsa; CCC_SE=ADC_CL2fMC1cf8dfSjuccno0cF1Ns%2bbtvKoi33Cs6QdUTFVJDFjj6uM%2bfLmmu6XX9A8yfKi4GXb1%2flxry3lyv3COAEfy8eYjv2kH3NNGANnPdsw%2ferNu%2baDyMBc43P3FW07io%2fwjN%2fmSKX5GBFoWWp%2bXGwV2JDCIoU1fl%2bjJ1OzJfOpiLEp8ovQFBrCVqClPMpAUnd4endpEuLf9uXiJCDWXupLmsqjB7LULXWzhqEIijouQcScUvumPUhHh0ZD%2bcbtoZw8dP1MVSlovcTmT%2fe22fbAlRJjNTPXCRXqUeQm3wxdSbz%2fimWqxjXfi%2bXLpV%2baspX2mIGL3RjekW5%2bfzqk8Q7ybxBrTzfGR6gzVnGamE0ooc4dfw8F%2fLIx0Ya4RxV%2ftBJdtnwmpOvgiy5Jep%2bA%2fafQJv8Y35pvdfS1VFagOa9XudkwXxw5fIYhTBC1Nn00BSjV6yT%2bci%2fh4Ign28gzp6GgAk9%2f0DACF5vqFIMJ5UFftuJYwhdS%2fsxULXk%2btDeEzQHbdawBLfwwi3i74zoYXN%2fRLyKNvW%2fp4tyJSZtEEdOhLRBxwS1DIUFtDrNs0wLzj7AuvvnMK5FlZJ4P5xFKMDyXQpEmFpd7tI1j9pvUutR%2b%2fnWz17DDvQFpCmD7HiinMFq3AhHSzfuA94gpQc9tOOmL2oShxI8yjaFNuib5WFats4%2fO9o4QxaJ0%2fenYBo16eQfrAOB2dE9xfgHhNaK0%2ftW55ONam3a4QNdqFfv8Gwqg%3d; __jdv=122270672|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_b43228c488d2411cb735c26b769d6851|1512896074664; __jda=122270672.1417778436.1512712379.1512901466.1512911995.8; __jdb=122270672.3.1417778436|8.1512911995; __jdc=122270672; __jdu=1417778436; 3AB9D23F7A4B3C9B=ZT3536NGESEGCF5E5MCBC53X6AQUQB7FNGC3UR7MBALPBWXLDN4XMVQGTDEL6SERTGUOFZHSCZU5VWZ2AHLAPZGACA");
			httpPost.setHeader("referer", "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=8720a017b65c44c3b16a1f0f6e276899");
			httpPost.setHeader("Upgrade-Insecure-Requests", "1");
			httpPost.setHeader("pragma", "no-cache");
		}
	}

	/**
	 * 设置http的HEAD
	 * 
	 * @param httpGet
	 * @param headMap
	 */
	private void setGetHead(HttpGet httpGet, Map<String, String> headMap) {
		if (headMap != null && headMap.size() > 0) {
			Set<String> keySet = headMap.keySet();
			for (String key : keySet) {
				httpGet.addHeader(key, headMap.get(key));
			}
		} else {
			// 设置默认的请求参数
			httpGet.setHeader("user-agent",	getUserAgent());
			// ...
			// 伪装成浏览器
			httpGet.setHeader(":authority", "item.jd.com");
			httpGet.setHeader(":method", "GET");
			httpGet.setHeader(":scheme", "https");
			httpGet.setHeader("User-Agent",	getUserAgent());
			httpGet.setHeader("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");
			httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
			httpGet.setHeader("Cache-Control", "max-age=0");
			httpGet.setHeader("Cookie",
					"ipLoc-djd=1-72-2799-0; mt_xid=V2_52007VwMXUlteVFMaSx5sAjUFQVNfXlpGGxtKXhliVhYHQVBbCBdVS1oBMwQRWltaB1MYeRpdBWEfElFBWVtLH00SXANsAhBiX2hSah9NGFgBYAUXVW1YV1wY; user-key=b9de3f4b-e58f-4091-9803-1dec084c7108; cn=0; unpl=V2_ZzNtbRFSQRZ2ARRRchAJBmIDEg8RUUERJwtBB30eVFBgChZdclRCFXMURldnGV0UZwQZWUdcQRdFCHZXchBYAWcCGllyBBNNIEwHDCRSBUE3XHxcFVUWF3RaTwEoSVoAYwtBDkZUFBYhW0IAKElVVTUFR21yVEMldQl2VH4ZWgNhChdfRWdzEkU4dlN6H1sAZDMTbUNnAUEpAEJcfR9fSGcGEltEUUoQdw92VUsa; CCC_SE=ADC_CL2fMC1cf8dfSjuccno0cF1Ns%2bbtvKoi33Cs6QdUTFVJDFjj6uM%2bfLmmu6XX9A8yfKi4GXb1%2flxry3lyv3COAEfy8eYjv2kH3NNGANnPdsw%2ferNu%2baDyMBc43P3FW07io%2fwjN%2fmSKX5GBFoWWp%2bXGwV2JDCIoU1fl%2bjJ1OzJfOpiLEp8ovQFBrCVqClPMpAUnd4endpEuLf9uXiJCDWXupLmsqjB7LULXWzhqEIijouQcScUvumPUhHh0ZD%2bcbtoZw8dP1MVSlovcTmT%2fe22fbAlRJjNTPXCRXqUeQm3wxdSbz%2fimWqxjXfi%2bXLpV%2baspX2mIGL3RjekW5%2bfzqk8Q7ybxBrTzfGR6gzVnGamE0ooc4dfw8F%2fLIx0Ya4RxV%2ftBJdtnwmpOvgiy5Jep%2bA%2fafQJv8Y35pvdfS1VFagOa9XudkwXxw5fIYhTBC1Nn00BSjV6yT%2bci%2fh4Ign28gzp6GgAk9%2f0DACF5vqFIMJ5UFftuJYwhdS%2fsxULXk%2btDeEzQHbdawBLfwwi3i74zoYXN%2fRLyKNvW%2fp4tyJSZtEEdOhLRBxwS1DIUFtDrNs0wLzj7AuvvnMK5FlZJ4P5xFKMDyXQpEmFpd7tI1j9pvUutR%2b%2fnWz17DDvQFpCmD7HiinMFq3AhHSzfuA94gpQc9tOOmL2oShxI8yjaFNuib5WFats4%2fO9o4QxaJ0%2fenYBo16eQfrAOB2dE9xfgHhNaK0%2ftW55ONam3a4QNdqFfv8Gwqg%3d; __jdv=122270672|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_b43228c488d2411cb735c26b769d6851|1512896074664; __jda=122270672.1417778436.1512712379.1512901466.1512911995.8; __jdb=122270672.3.1417778436|8.1512911995; __jdc=122270672; __jdu=1417778436; 3AB9D23F7A4B3C9B=ZT3536NGESEGCF5E5MCBC53X6AQUQB7FNGC3UR7MBALPBWXLDN4XMVQGTDEL6SERTGUOFZHSCZU5VWZ2AHLAPZGACA");
			httpGet.setHeader("referer", "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=8720a017b65c44c3b16a1f0f6e276899");
			httpGet.setHeader("Upgrade-Insecure-Requests", "1");
			httpGet.setHeader("pragma", "no-cache");
		}
	}
	
	
	private String getUserAgent(){
		
		ArrayList<String> list = new ArrayList<String>(){{
			 add("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2228.0 Safari/537.36");
			 add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2227.1 Safari/537.36");
			 add("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2227.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2227.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2226.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2225.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2225.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/41.0.2224.3 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/40.0.2214.93 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/40.0.2214.93 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/37.0.2049.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/37.0.2049.0 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/36.0.1985.67 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/36.0.1985.67 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/35.0.3319.102 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/35.0.2309.372 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/35.0.2117.157 Safari/537.36");
			 add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/35.0.1916.47 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML); like Gecko) Chrome/34.0.1866.237 Safari/537.36");
			 add("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
		 }};
		
		 int index = new Random().nextInt( list.size() );
		 return list.get(index);
	}

	/**
	 * 上传文件
	 * 
	 * @param serverUrl
	 *            服务器地址
	 * @param localFilePath
	 *            本地文件路径
	 * @param serverFieldName
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public String uploadFileImpl(String serverUrl, String localFilePath, String serverFieldName,
			Map<String, String> params) throws Exception {
		String respStr = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httppost = new HttpPost(serverUrl);

			// 设置超时时间
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(50000)
					.setConnectionRequestTimeout(10000).setSocketTimeout(50000).build();
			httppost.setConfig(requestConfig);

			FileBody binFileBody = new FileBody(new File(localFilePath));

			MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
			// add the file params
			multipartEntityBuilder.addPart(serverFieldName, binFileBody);
			// 设置上传的其他参数
			setUploadParams(multipartEntityBuilder, params);

			HttpEntity reqEntity = multipartEntityBuilder.build();
			httppost.setEntity(reqEntity);

			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				System.out.println(response.getStatusLine());
				HttpEntity resEntity = response.getEntity();
				respStr = getRespString(resEntity);
				EntityUtils.consume(resEntity);
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
		System.out.println("resp=" + respStr);
		return respStr;
	}

	/**
	 * 设置上传文件时所附带的其他参数
	 * 
	 * @param multipartEntityBuilder
	 * @param params
	 */
	private void setUploadParams(MultipartEntityBuilder multipartEntityBuilder, Map<String, String> params) {
		if (params != null && params.size() > 0) {
			Set<String> keys = params.keySet();
			for (String key : keys) {
				multipartEntityBuilder.addPart(key, new StringBody(params.get(key), ContentType.TEXT_PLAIN));
			}
		}
	}

	/**
	 * 将返回结果转化为String
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	private String getRespString(HttpEntity entity) throws Exception {
		if (entity == null) {
			return null;
		}
		// InputStream is = entity.getContent();
		// 定义BufferedReader输入流来读取URL的响应
		BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent(), charSet));
		StringBuffer strBuf = new StringBuffer();
		String line = null;
		while ((line = in.readLine()) != null) {
			strBuf.append(line);
		}
		return strBuf.toString();
	}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.test</groupId>
	<artifactId>spider-base</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<!-- httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>fluent-hc</artifactId>
			<version>4.5.3</version>
		</dependency>
		<!-- junit单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
		</dependency>
		<!-- jsoup -->
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.8.3</version>
		</dependency>
		<!-- 文件上传类 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.5.2</version>
		</dependency>

		<!-- <dependency> -->
		<!-- <groupId>org.seleniumhq.selenium</groupId> -->
		<!-- <artifactId>selenium-java</artifactId> -->
		<!-- <version>3.4.0</version> -->
		<!-- </dependency> -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.39</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值