EciHttpUtil.java
7.63 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.pashanhoo.common;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
public class EciHttpUtil {
private static Logger logger = LoggerFactory.getLogger(EciHttpUtil.class);
@Value("${app.AppToken}")
private String appToken;
@Value("${app.AppSecret}")
private String appSecret;
@Value("${app.hostUrl}")
private String hostUrl;
public Map doPostForNew(String url, String params) {
logger.info("http request url:" + url);
logger.info("http request url:" + params);
PrintWriter out = null;
BufferedReader in = null;
Map jsonObject = new HashMap();
try{
URL url1=new URL(url);
HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("accept","*/*");
conn.setRequestProperty("connection","Keep-Alive");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
long times=System.currentTimeMillis();
String unSignature = appToken + appSecret + times;
String signature = MD5Util.getMD5(unSignature);
conn.setRequestProperty("x-qys-accesstoken", appToken);
conn.setRequestProperty("x-qys-signature", signature);
conn.setRequestProperty("x-qys-timestamp", String.valueOf(times));
logger.info("x-qys-accesstoken==="+appToken);
logger.info("x-qys-signature==="+signature);
logger.info("x-qys-timestamp==="+System.currentTimeMillis());
out=new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
out.print(params);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String result="";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
jsonObject = JSONObject.parseObject(result, Map.class);
}catch (Exception ex){
logger.info("发送 POST 请求出现异常!" + ex);
ex.printStackTrace();
}finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return jsonObject;
}
public Map doGetForNew(String url) {
logger.info("http request url:" + url);
BufferedReader in = null;
String result = "";
Map jsonObject = new HashMap();
try{
URL url1=new URL(url);
HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("accept","*/*");
conn.setRequestProperty("connection","Keep-Alive");
conn.setRequestProperty("Charset", "utf-8");
conn.setDoInput(true);
conn.setDoOutput(false);
//x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
long times=System.currentTimeMillis();
String unSignature = appToken +appSecret + times;
String signature = MD5Util.getMD5(unSignature);
conn.setRequestProperty("x-qys-accesstoken", appToken);
conn.setRequestProperty("x-qys-signature", signature);
conn.setRequestProperty("x-qys-timestamp", String.valueOf(times));
logger.info("x-qys-accesstoken==="+appToken);
logger.info("x-qys-signature==="+signature);
logger.info("x-qys-timestamp==="+System.currentTimeMillis());
conn.connect();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
jsonObject = JSONObject.parseObject(result, Map.class);
}catch (Exception ex){
logger.info("发送 POST 请求出现异常!" + ex);
ex.printStackTrace();
}finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return jsonObject;
}
/**
* 文件下载
*
* @param documentId 合同文档ID
* @return
*/
public String download(String documentId,String bh) {
String url = hostUrl + "/document/download" + "?documentId=" + documentId;
InputStream in = null;
String absolutePath = "";
OutputStream out = null;
try{
URL url1=new URL(url);
HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("accept","*/*");
conn.setRequestProperty("connection","Keep-Alive");
conn.setRequestProperty("Charset", "utf-8");
conn.setDoInput(true);
conn.setDoOutput(true);
long times=System.currentTimeMillis();
//x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
String unSignature = appToken + appSecret + times;
String signature = MD5Util.getMD5(unSignature);
conn.setRequestProperty("x-qys-accesstoken", appToken);
conn.setRequestProperty("x-qys-signature", signature);
conn.setRequestProperty("x-qys-timestamp", String.valueOf(times));
logger.info("x-qys-accesstoken==="+appToken);
logger.info("x-qys-signature==="+signature);
logger.info("x-qys-timestamp==="+System.currentTimeMillis());
conn.connect();
in =conn.getInputStream();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
String dateDir = simpleDateFormat.format(new Date());
File savePath = new File("D:\\ECI\\" + dateDir+bh);
if (!savePath.exists()) {
savePath.mkdirs();
}
File filePath = new File(savePath + "\\" + documentId+".ofd");
if (!filePath.exists()){
filePath.createNewFile();
}
out = new FileOutputStream(filePath);
byte[] buffer = new byte[4096];
int readLength = 0;
while ((readLength = in.read(buffer)) != -1) {
out.write(buffer, 0, readLength);
}
absolutePath = filePath.getAbsolutePath();
out.flush();
}catch (Exception ex){
logger.info("发送 POST 请求出现异常!" + ex);
ex.printStackTrace();
}finally {
try {
if (in != null) {
in.close();
}
if(out != null) {
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return absolutePath;
}
}