package com.yanzuoguang.cloud.helper; import com.alibaba.fastjson.JSON; import com.yanzuoguang.util.vo.ResponseResult; 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.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; /** * HTTP客户端上传文件 * @author 颜佐光 */ public final class HttpUploadHelper { private static final Logger logger = LoggerFactory.getLogger(HttpUploadHelper.class); public static final String TAG_NAME = "file"; public static final int CONNECT_TIME_OUT = 200000; public static final int SOCKET_TIME_OUT = 200000; /** * 上传文件 * * @param url 上传的文件 * @param filePath 上传的路径 * @return */ public static String upload(final String url, final String filePath) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httpPost = initHttpPost(url); httpPost.setEntity(initRequestEntity(filePath)); logger.debug("executing request: {}", httpPost.getRequestLine()); CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpPost); HttpEntity responseEntity = closeableHttpResponse.getEntity(); if (responseEntity != null) { String result = EntityUtils.toString(closeableHttpResponse.getEntity()); logger.debug("response result: {}", result); EntityUtils.consume(responseEntity); return result; } closeableHttpResponse.close(); return JSON.toJSONString(initResponseError()); } catch (ClientProtocolException e) { logger.error("exception ClientProtocolException.", e); throw new RuntimeException(e); } catch (IOException e) { logger.error("exception IOException.", e); throw new RuntimeException(e); } finally { try { httpclient.close(); } catch (IOException e) { logger.error("close IO exception IOException.", e); throw new RuntimeException(e); } } } /** * 初始化HttpPost * * @param url * @return */ private static HttpPost initHttpPost(String url) { HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIME_OUT).setSocketTimeout(SOCKET_TIME_OUT).build(); httpPost.setConfig(requestConfig); return httpPost; } /** * 初始化请求HttpEntity * * @param filePath * @return */ private static HttpEntity initRequestEntity(String filePath) { FileBody fileBody = new FileBody(new File(filePath)); StringBody stringBody = new StringBody("This is comment", ContentType.TEXT_PLAIN); return MultipartEntityBuilder.create().addPart(TAG_NAME, fileBody).addPart("comment", stringBody).build(); } /** * 初始化返回错误信息 * * @return */ private static ResponseResult initResponseError() { return ResponseResult.error("01", "上传文件失败"); } }