1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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", "上传文件失败");
}
}