package com.product.file.util; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpTest { protected static Logger logger = LoggerFactory.getLogger(HttpTest.class); static String uri = "http://127.0.0.1:9998/lx"; private static final String ACCESS_TOKEN = "token"; private static final String language="zh-CN";//zh-CN en private static final String systemClientVersion="1.0.0"; private static final String systemClientType="Web"; /** * 支持文件上传的接口调用 */ public static void testFileUpload(Map map) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { CloseableHttpClient client = null; CloseableHttpResponse response = null; Object token = map.get("token"); Object cookies = map.get("cookie"); String cookie[] = new String[2]; if (token == null) { cookie = com.product.common.utils.HttpTest.login(); } else { cookie[0] = cookies.toString(); cookie[1] = token.toString(); } logger.info("cookie[0]="+cookie[0]+"------cookie[1]="+cookie[1]+"------------------------------------"); if (cookie == null || cookie[1] == null) { return; } String url = null; Map fileMap = null; JSONObject jsonParam = new JSONObject(); for (String key : map.keySet()) {// keySet获取map集合key的集合  然后在遍历key即可 if ("url/api".equals(key.trim())) { url = map.get(key).toString(); } else if ("data".equals(key)) { Map value = (Map) map.get(key);// jsonParam.put(key, value); } else if ("fileMap".equals(key)) { fileMap = (Map) map.get(key); } else if (map.get(key) instanceof List) { List value = (List) map.get(key);// jsonParam.put(key, value); } else { jsonParam.put(key, map.get(key)); } } jsonParam.put(ACCESS_TOKEN, cookie[1]); List parames = new ArrayList<>(); logger.info("上传json:" + jsonParam.toString()); parames.add(new BasicNameValuePair("formData", jsonParam.toString())); HttpPost httppost = new HttpPost(uri + url); httppost.addHeader("Cookie", cookie[0]); httppost.addHeader("system-language-code", HttpTest.language); httppost.addHeader("system-client-type", HttpTest.systemClientType); httppost.addHeader("system-client-version", HttpTest.systemClientVersion); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000) .build(); httppost.setConfig(requestConfig); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); for (Map.Entry entry : fileMap.entrySet()) { multipartEntityBuilder.addPart(entry.getKey(), new FileBody(new File(entry.getValue()))); } StringBody comment = new StringBody(jsonParam.toString(), ContentType.TEXT_PLAIN); multipartEntityBuilder.addPart("formData", comment); HttpEntity reqEntity = multipartEntityBuilder.build(); System.out.println("上传参数:" + jsonParam); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); response = httpclient.execute(httppost); try { System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { String responseEntityStr = EntityUtils.toString(response.getEntity()); System.out.println(responseEntityStr); System.out.println("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 支持文件下载的接口调用 */ public static void testFileDownload(Map map) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { CloseableHttpResponse response = null; Object token = map.get("token"); Object cookies = map.get("cookie"); String cookie[] = new String[2]; if (token == null) { cookie = com.product.common.utils.HttpTest.login(); } else { cookie[0] = cookies.toString(); cookie[1] = token.toString(); } logger.info("cookie[0]="+cookie[0]+"------cookie[1]="+cookie[1]+"------------------------------------"); if (cookie == null || cookie[1] == null) { return; } String url = null; JSONObject jsonParam = new JSONObject(); for (String key : map.keySet()) {// keySet获取map集合key的集合  然后在遍历key即可 if ("url/api".equals(key.trim())) { url = map.get(key).toString(); } else if ("data".equals(key)) { Map value = (Map) map.get(key);// jsonParam.put(key, value); } else if (map.get(key) instanceof List) { List value = (List) map.get(key);// jsonParam.put(key, value); } else { jsonParam.put(key, map.get(key)); } } jsonParam.put(ACCESS_TOKEN, cookie[1]); List parames = new ArrayList<>(); logger.info("上传json:" + jsonParam.toString()); parames.add(new BasicNameValuePair("formData", jsonParam.toString())); HttpPost httppost = new HttpPost(uri + url); httppost.addHeader("Cookie", cookie[0]); httppost.addHeader("system-language-code", HttpTest.language); httppost.addHeader("system-client-type", HttpTest.systemClientType); httppost.addHeader("system-client-version", HttpTest.systemClientVersion); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build(); httppost.setConfig(requestConfig); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); StringBody comment = new StringBody(jsonParam.toString(), ContentType.TEXT_PLAIN); multipartEntityBuilder.addPart("formData", comment); HttpEntity reqEntity = multipartEntityBuilder.build(); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); response = httpclient.execute(httppost); try { System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println(reqEntity.getContentLength()); System.out.println(reqEntity.getContentType()); // String responseEntityStr = EntityUtils.toString(response.getEntity()); // System.out.println(responseEntityStr); // System.out.println("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } }