EciHttpUtil.java
7.06 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.thinkgem.jeesite.modules.eci.utils;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.exception.BussException;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
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.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class EciHttpUtil {
private static Logger logger = LoggerFactory.getLogger(EciHttpUtil.class);
/**
* post请求接口地址
*
* @param url
* @param params
* @return
* @throws Exception
*/
public static String doPost(String url, Map params) {
logger.info("http request url:" + url);
logger.info("http request data:" + JsonUtil.toJsonString(params));
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> urlParameters = Lists.newArrayList();
String result = null;
//创建通道
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
try {
if (params != null && params.size() > 0) {
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
urlParameters.add(new BasicNameValuePair(key, params.get(key).toString()));
}
httpPost.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
}
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
//x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
String unSignature = Global.ECI_APP_TOKEN + Global.ECI_APP_SECRET + System.currentTimeMillis();
String signature = MD5Util.getMD5(unSignature);
httpPost.setHeader("x-qys-signature", signature);
response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
result = EntityUtils.toString(entity, "utf-8");
} else {
throw new BussException("返回状态码:" + code);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
try {
response.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* get请求接口地址
*
* @param url
* @param
* @return
* @throws Exception
*/
public static String doGet(String url) {
HttpGet httpGet = new HttpGet(url);
String result = null;
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
try {
httpGet.setHeader("Content-Type", "application/json;charset=utf-8");
//x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
String unSignature = Global.ECI_APP_TOKEN + Global.ECI_APP_SECRET + System.currentTimeMillis();
String signature = MD5Util.getMD5(unSignature);
httpGet.setHeader("x-qys-signature", signature);
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
result = EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
try {
response.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 文件下载
*
* @param documentId 合同文档ID
* @return
*/
public static String download(String documentId) {
String url = Global.ECI_HOST + "/document/download" + "?documentId=" + documentId;
HttpGet httpGet = new HttpGet(url);
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
OutputStream out = null;
InputStream in = null;
String path = null;
try {
httpGet.setHeader("Content-Type", "application/json;charset=utf-8");
//x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
String unSignature = Global.ECI_APP_TOKEN + Global.ECI_APP_SECRET + System.currentTimeMillis();
String signature = MD5Util.getMD5(unSignature);
httpGet.setHeader("x-qys-signature", signature);
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
in = entity.getContent();
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
long length = entity.getContentLength();
if (length > 0) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dddd");
String dateDir = simpleDateFormat.format(new Date());
File file = new File("E:\\ECI\\" + dateDir + "\\" + documentId + ".ofd");
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int readLength = 0;
while ((readLength = in.read(buffer)) != -1) {
// byte[] bytes = new byte[readLength];
// System.arraycopy(buffer, 0, bytes, 0, readLength);
out.write(buffer, 0, readLength);
}
path = file.getAbsolutePath();
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
try {
response.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return path;
}
}