提交 | 用户 | age
|
826b66
|
1 |
package com.hx.mp.util; |
C |
2 |
|
|
3 |
import com.hx.util.OSSUtil; |
|
4 |
import com.hx.util.SimpleTool; |
|
5 |
import com.hx.util.StringUtils; |
|
6 |
import com.hz.util.http.HttpHzUtil; |
|
7 |
import com.hz.util.http.dto.HttpHzResponse; |
|
8 |
import net.sf.json.JSONException; |
|
9 |
import net.sf.json.JSONObject; |
|
10 |
import org.apache.commons.codec.binary.Base64; |
|
11 |
import org.apache.commons.io.FileUtils; |
|
12 |
import org.apache.commons.io.IOUtils; |
|
13 |
import org.springframework.stereotype.Component; |
|
14 |
|
|
15 |
import java.io.*; |
|
16 |
import java.net.HttpURLConnection; |
|
17 |
import java.net.URL; |
|
18 |
import java.nio.charset.StandardCharsets; |
|
19 |
import java.text.MessageFormat; |
|
20 |
import java.text.SimpleDateFormat; |
|
21 |
import java.util.Date; |
|
22 |
|
|
23 |
/** |
|
24 |
* 微信获取权限基本工具类 |
|
25 |
* @author ChenJiaHe |
|
26 |
* @Date 2020-07-14 |
|
27 |
*/ |
|
28 |
@Component |
|
29 |
public class MPWeixinBaseUtil { |
|
30 |
|
|
31 |
// 类型 |
|
32 |
private static final String GRANT_TYPE = "client_credential"; |
|
33 |
|
|
34 |
/**获取access_token链接*/ |
|
35 |
private static final String GETACCESS_TOKENURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type="; |
|
36 |
/**通过code获取小程序信息链接*/ |
|
37 |
private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?"; |
|
38 |
/**发送订阅消息通知链接*/ |
|
39 |
private static final String SEND_SUBSCRIBE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; |
|
40 |
/**生成小程序二维码地址(方形)*/ |
|
41 |
public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; |
|
42 |
/**生成小程序二维码地址(圆形)*/ |
|
43 |
public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}"; |
|
44 |
/**生成无限二维码*/ |
|
45 |
public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; |
|
46 |
/**获取小程序订阅个人模板列表*/ |
|
47 |
private static final String GET_APP_TEMPLATE = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token="; |
|
48 |
/**获取公众号模板列表*/ |
|
49 |
private static final String GET_GZH_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="; |
|
50 |
////////////////////////////////////////////////// |
|
51 |
|
|
52 |
/** (小程序)通过code换取网页授权access_token/如果是snsapi_base模式的授权,这里就可以拿到openId了 ***/ |
|
53 |
public static JSONObject getJscode2session(String appid,String secret,String code) { |
|
54 |
|
|
55 |
return HttpURLUtil(JSCODE2SESSION_URL+"appid="+appid+"&secret="+secret |
|
56 |
+"&grant_type=authorization_code&js_code=" + code,null); |
|
57 |
} |
|
58 |
|
|
59 |
/**发送订阅消息通知**/ |
|
60 |
public static JSONObject sendSubscribeMessage(String data,String accessToken){ |
|
61 |
return HttpURLUtil(SEND_SUBSCRIBE_MESSAGE+accessToken,data); |
|
62 |
} |
|
63 |
|
|
64 |
/** 获取access_token */ |
|
65 |
public static JSONObject getAccessToken(String appid,String secret) throws Exception { |
|
66 |
String access_token; |
|
67 |
// 通过WX接口获取access_token |
|
68 |
JSONObject obj = getApplication_Access_tokenForUrl(appid,secret); |
|
69 |
return obj; |
|
70 |
} |
|
71 |
|
|
72 |
/** 从weixin接口获取Access_token **/ |
|
73 |
public static JSONObject getApplication_Access_tokenForUrl(String appid,String secret) { |
|
74 |
return HttpURLUtil(GETACCESS_TOKENURL+GRANT_TYPE +"&appid="+appid +"&secret="+secret,null); |
|
75 |
} |
|
76 |
|
|
77 |
/**生成无限二维码,并上传到OSS*/ |
|
78 |
public static String createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
79 |
JSONObject lineColor, boolean isHyaline, String fileName, String keyId, |
|
80 |
String keySecret, String endPoint, String bucket) |
|
81 |
{ |
|
82 |
String imgUrl = null; |
|
83 |
InputStream in = null; |
|
84 |
HttpURLConnection conn = null; |
|
85 |
|
|
86 |
try { |
|
87 |
//生成发送数据 |
|
88 |
JSONObject obj = new JSONObject(); |
|
89 |
obj.put("scene", scene); |
|
90 |
obj.put("width", width); |
|
91 |
obj.put("page", page); |
|
92 |
obj.put("auto_color", autoColor); |
|
93 |
obj.put("line_color", lineColor); |
|
94 |
obj.put("is_hyaline", isHyaline); |
|
95 |
|
|
96 |
// 创建url资源 |
|
97 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
98 |
// 建立http连接 |
|
99 |
conn = (HttpURLConnection) url.openConnection(); |
|
100 |
// 设置允许输出 |
|
101 |
conn.setDoOutput(true); |
|
102 |
conn.setDoInput(true); |
|
103 |
// 设置不用缓存 |
|
104 |
conn.setUseCaches(false); |
|
105 |
// 设置传递方式 |
|
106 |
conn.setRequestMethod("POST"); |
|
107 |
// 设置维持长连接 |
|
108 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
109 |
// 设置文件字符集: |
|
110 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
111 |
// 设置文件类型: |
|
112 |
conn.setRequestProperty("contentType", "application/json"); |
|
113 |
// 开始连接请求 |
|
114 |
conn.connect(); |
|
115 |
OutputStream out = conn.getOutputStream(); |
|
116 |
// 写入请求的字符串 |
|
117 |
out.write((obj.toString()).getBytes()); |
|
118 |
out.flush(); |
|
119 |
out.close(); |
|
120 |
|
|
121 |
// 请求返回的状态 |
|
122 |
if (conn.getResponseCode() == 200) { |
|
123 |
// 请求返回的数据 |
|
124 |
in = conn.getInputStream(); |
|
125 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
126 |
conn.disconnect(); |
|
127 |
conn = null; |
|
128 |
} |
|
129 |
|
|
130 |
if (in != null) { |
|
131 |
in.close(); |
|
132 |
in = null; |
|
133 |
} |
|
134 |
|
|
135 |
if (conn != null) { |
|
136 |
conn.disconnect(); |
|
137 |
conn = null; |
|
138 |
} |
|
139 |
}catch (Exception e) |
|
140 |
{ |
|
141 |
e.printStackTrace(); |
|
142 |
|
|
143 |
if (in != null) { |
|
144 |
try { |
|
145 |
in.close(); |
|
146 |
}catch (Exception ep) |
|
147 |
{ |
|
148 |
ep.printStackTrace(); |
|
149 |
} |
|
150 |
in = null; |
|
151 |
} |
|
152 |
|
|
153 |
if (conn != null) { |
|
154 |
conn.disconnect(); |
|
155 |
conn = null; |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
return imgUrl; |
|
160 |
} |
|
161 |
|
|
162 |
/**生成有限二维码,返回临时文 |
|
163 |
* @param at 微信token |
|
164 |
* @param page 跳转链接 |
|
165 |
* @param width 宽度 |
|
166 |
* @param autoColor 默认false |
|
167 |
* @param lineColor 默认null |
|
168 |
* @param isHyaline 默认false |
|
169 |
* @return |
|
170 |
*/ |
|
171 |
public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, |
|
172 |
JSONObject lineColor, boolean isHyaline) { |
|
173 |
String imgUrl = null; |
|
174 |
InputStream in = null; |
|
175 |
HttpURLConnection conn = null; |
|
176 |
|
|
177 |
//创建临时文件 |
|
178 |
File file = null; |
|
179 |
try { |
|
180 |
file = File.createTempFile("temp", ".jpg"); |
|
181 |
//生成发送数据 |
|
182 |
JSONObject obj = new JSONObject(); |
|
183 |
obj.put("width", width); |
|
184 |
obj.put("path", page); |
|
185 |
obj.put("auto_color", autoColor); |
|
186 |
obj.put("line_color", lineColor); |
|
187 |
obj.put("is_hyaline", isHyaline); |
|
188 |
|
|
189 |
// 创建url资源 |
|
190 |
URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); |
|
191 |
// 建立http连接 |
|
192 |
conn = (HttpURLConnection) url.openConnection(); |
|
193 |
// 设置允许输出 |
|
194 |
conn.setDoOutput(true); |
|
195 |
conn.setDoInput(true); |
|
196 |
// 设置不用缓存 |
|
197 |
conn.setUseCaches(false); |
|
198 |
// 设置传递方式 |
|
199 |
conn.setRequestMethod("POST"); |
|
200 |
// 设置维持长连接 |
|
201 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
202 |
// 设置文件字符集: |
|
203 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
204 |
// 设置文件类型: |
|
205 |
conn.setRequestProperty("contentType", "application/json"); |
|
206 |
// 开始连接请求 |
|
207 |
conn.connect(); |
|
208 |
OutputStream out = conn.getOutputStream(); |
|
209 |
// 写入请求的字符串 |
|
210 |
out.write((obj.toString()).getBytes()); |
|
211 |
out.flush(); |
|
212 |
out.close(); |
|
213 |
|
|
214 |
// 请求返回的状态 |
|
215 |
if (conn.getResponseCode() == 200) { |
|
216 |
// 请求返回的数据 |
|
217 |
in = conn.getInputStream(); |
|
218 |
//输入到临时文件 |
|
219 |
/*OutputStream os = new FileOutputStream(file); |
|
220 |
int bytesRead = 0; |
|
221 |
byte[] buffer = new byte[8192]; |
|
222 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
223 |
os.write(buffer, 0, bytesRead); |
|
224 |
}*/ |
|
225 |
FileUtils.copyInputStreamToFile(in, file); |
|
226 |
|
|
227 |
conn.disconnect(); |
|
228 |
conn = null; |
|
229 |
} |
|
230 |
|
|
231 |
if (in != null) { |
|
232 |
in.close(); |
|
233 |
in = null; |
|
234 |
} |
|
235 |
|
|
236 |
if (conn != null) { |
|
237 |
conn.disconnect(); |
|
238 |
conn = null; |
|
239 |
} |
|
240 |
}catch (Exception e) { |
|
241 |
e.printStackTrace(); |
|
242 |
if (in != null) { |
|
243 |
try { |
|
244 |
in.close(); |
|
245 |
}catch (Exception ep) { |
|
246 |
ep.printStackTrace(); |
|
247 |
} |
|
248 |
in = null; |
|
249 |
} |
|
250 |
|
|
251 |
if (conn != null) { |
|
252 |
conn.disconnect(); |
|
253 |
conn = null; |
|
254 |
} |
|
255 |
}finally { |
|
256 |
if(file != null){ |
|
257 |
file.deleteOnExit(); |
|
258 |
} |
|
259 |
} |
|
260 |
return file; |
|
261 |
} |
|
262 |
|
|
263 |
|
|
264 |
/**生成有限二维码,返回临时文 |
|
265 |
* @param at 微信token |
|
266 |
* @param page 跳转链接 |
|
267 |
* @param width 宽度 |
|
268 |
* @param autoColor 默认false |
|
269 |
* @param lineColor 默认null |
|
270 |
* @param isHyaline 默认false |
|
271 |
* @param envVersion 要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop |
|
272 |
* @return |
|
273 |
*/ |
|
274 |
public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, |
|
275 |
JSONObject lineColor, boolean isHyaline,String envVersion) { |
|
276 |
String imgUrl = null; |
|
277 |
InputStream in = null; |
|
278 |
HttpURLConnection conn = null; |
|
279 |
|
|
280 |
//创建临时文件 |
|
281 |
File file = null; |
|
282 |
try { |
|
283 |
file = File.createTempFile("temp", ".jpg"); |
|
284 |
//生成发送数据 |
|
285 |
JSONObject obj = new JSONObject(); |
|
286 |
obj.put("width", width); |
|
287 |
obj.put("path", page); |
|
288 |
obj.put("auto_color", autoColor); |
|
289 |
obj.put("line_color", lineColor); |
|
290 |
obj.put("is_hyaline", isHyaline); |
|
291 |
obj.put("env_version", envVersion); |
|
292 |
|
|
293 |
// 创建url资源 |
|
294 |
URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); |
|
295 |
// 建立http连接 |
|
296 |
conn = (HttpURLConnection) url.openConnection(); |
|
297 |
// 设置允许输出 |
|
298 |
conn.setDoOutput(true); |
|
299 |
conn.setDoInput(true); |
|
300 |
// 设置不用缓存 |
|
301 |
conn.setUseCaches(false); |
|
302 |
// 设置传递方式 |
|
303 |
conn.setRequestMethod("POST"); |
|
304 |
// 设置维持长连接 |
|
305 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
306 |
// 设置文件字符集: |
|
307 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
308 |
// 设置文件类型: |
|
309 |
conn.setRequestProperty("contentType", "application/json"); |
|
310 |
// 开始连接请求 |
|
311 |
conn.connect(); |
|
312 |
OutputStream out = conn.getOutputStream(); |
|
313 |
// 写入请求的字符串 |
|
314 |
out.write((obj.toString()).getBytes()); |
|
315 |
out.flush(); |
|
316 |
out.close(); |
|
317 |
|
|
318 |
// 请求返回的状态 |
|
319 |
if (conn.getResponseCode() == 200) { |
|
320 |
// 请求返回的数据 |
|
321 |
in = conn.getInputStream(); |
|
322 |
//输入到临时文件 |
|
323 |
/*OutputStream os = new FileOutputStream(file); |
|
324 |
int bytesRead = 0; |
|
325 |
byte[] buffer = new byte[8192]; |
|
326 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
327 |
os.write(buffer, 0, bytesRead); |
|
328 |
}*/ |
|
329 |
FileUtils.copyInputStreamToFile(in, file); |
|
330 |
|
|
331 |
conn.disconnect(); |
|
332 |
conn = null; |
|
333 |
} |
|
334 |
|
|
335 |
if (in != null) { |
|
336 |
in.close(); |
|
337 |
in = null; |
|
338 |
} |
|
339 |
|
|
340 |
if (conn != null) { |
|
341 |
conn.disconnect(); |
|
342 |
conn = null; |
|
343 |
} |
|
344 |
}catch (Exception e) { |
|
345 |
e.printStackTrace(); |
|
346 |
if (in != null) { |
|
347 |
try { |
|
348 |
in.close(); |
|
349 |
}catch (Exception ep) { |
|
350 |
ep.printStackTrace(); |
|
351 |
} |
|
352 |
in = null; |
|
353 |
} |
|
354 |
|
|
355 |
if (conn != null) { |
|
356 |
conn.disconnect(); |
|
357 |
conn = null; |
|
358 |
} |
|
359 |
}finally { |
|
360 |
if(file != null){ |
|
361 |
file.deleteOnExit(); |
|
362 |
} |
|
363 |
} |
|
364 |
return file; |
|
365 |
} |
|
366 |
|
|
367 |
/**生成无限二维码,返回临时文 |
|
368 |
* @param at 微信token |
|
369 |
* @param scene 参数,只能32位,最好不要中文 |
|
370 |
* @param page 跳转链接 |
|
371 |
* @param width 宽度 |
|
372 |
* @param autoColor 默认false |
|
373 |
* @param lineColor 默认null |
|
374 |
* @param isHyaline 默认false |
|
375 |
* @return |
|
376 |
*/ |
|
377 |
public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
378 |
JSONObject lineColor, boolean isHyaline) throws IOException { |
|
379 |
String imgUrl = null; |
|
380 |
InputStream in = null; |
|
381 |
HttpURLConnection conn = null; |
|
382 |
|
|
383 |
//创建临时文件 |
|
384 |
File file = File.createTempFile("temp", ".jpg"); |
|
385 |
|
|
386 |
try { |
|
387 |
//生成发送数据 |
|
388 |
JSONObject obj = new JSONObject(); |
|
389 |
obj.put("scene", scene); |
|
390 |
obj.put("width", width); |
|
391 |
obj.put("page", page); |
|
392 |
obj.put("auto_color", autoColor); |
|
393 |
obj.put("line_color", lineColor); |
|
394 |
obj.put("is_hyaline", isHyaline); |
|
395 |
|
|
396 |
// 创建url资源 |
|
397 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
398 |
// 建立http连接 |
|
399 |
conn = (HttpURLConnection) url.openConnection(); |
|
400 |
// 设置允许输出 |
|
401 |
conn.setDoOutput(true); |
|
402 |
conn.setDoInput(true); |
|
403 |
// 设置不用缓存 |
|
404 |
conn.setUseCaches(false); |
|
405 |
// 设置传递方式 |
|
406 |
conn.setRequestMethod("POST"); |
|
407 |
// 设置维持长连接 |
|
408 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
409 |
// 设置文件字符集: |
|
410 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
411 |
// 设置文件类型: |
|
412 |
conn.setRequestProperty("contentType", "application/json"); |
|
413 |
// 开始连接请求 |
|
414 |
conn.connect(); |
|
415 |
OutputStream out = conn.getOutputStream(); |
|
416 |
// 写入请求的字符串 |
|
417 |
out.write((obj.toString()).getBytes()); |
|
418 |
out.flush(); |
|
419 |
out.close(); |
|
420 |
|
|
421 |
// 请求返回的状态 |
|
422 |
if (conn.getResponseCode() == 200) { |
|
423 |
// 请求返回的数据 |
|
424 |
in = conn.getInputStream(); |
|
425 |
//输入到临时文件 |
|
426 |
/*OutputStream os = new FileOutputStream(file); |
|
427 |
int bytesRead = 0; |
|
428 |
byte[] buffer = new byte[8192]; |
|
429 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
430 |
os.write(buffer, 0, bytesRead); |
|
431 |
}*/ |
|
432 |
FileUtils.copyInputStreamToFile(in, file); |
|
433 |
|
|
434 |
conn.disconnect(); |
|
435 |
conn = null; |
|
436 |
} |
|
437 |
|
|
438 |
if (in != null) { |
|
439 |
in.close(); |
|
440 |
in = null; |
|
441 |
} |
|
442 |
|
|
443 |
if (conn != null) { |
|
444 |
conn.disconnect(); |
|
445 |
conn = null; |
|
446 |
} |
|
447 |
}catch (Exception e) { |
|
448 |
e.printStackTrace(); |
|
449 |
if (in != null) { |
|
450 |
try { |
|
451 |
in.close(); |
|
452 |
}catch (Exception ep) { |
|
453 |
ep.printStackTrace(); |
|
454 |
} |
|
455 |
in = null; |
|
456 |
} |
|
457 |
|
|
458 |
if (conn != null) { |
|
459 |
conn.disconnect(); |
|
460 |
conn = null; |
|
461 |
} |
|
462 |
}finally { |
|
463 |
file.deleteOnExit(); |
|
464 |
} |
|
465 |
return file; |
|
466 |
} |
|
467 |
|
|
468 |
|
|
469 |
/**生成小程序二维码工具(方形) |
|
470 |
* path 二维码跳转链接 |
|
471 |
* width 二维码宽度,默认是430 |
|
472 |
* saveUrl 保存图片的全路径 |
|
473 |
* seeUrl 显示图片路径 |
|
474 |
* appid 小程序配置 |
|
475 |
* secret 小程序配置 |
|
476 |
* return 二维码显示图片链接 |
|
477 |
* @throws Exception |
|
478 |
* */ |
|
479 |
public static String twoCodeImgSquare(String at, String path,Integer width,String saveUrl, |
|
480 |
String seeUrl,String appid,String secret) throws Exception { |
|
481 |
|
|
482 |
if(!SimpleTool.checkNotNull(width)) { |
|
483 |
width = 430; |
|
484 |
} |
|
485 |
//生成发送数据 |
|
486 |
JSONObject obj = new JSONObject(); |
|
487 |
obj.put("path", path); |
|
488 |
obj.put("width", width); |
|
489 |
|
|
490 |
//二维码图片路径 |
|
491 |
String codeUrl = ""; |
|
492 |
codeUrl = HttpURLUtilMakeCodeImg(MAKE_TWOCODE_SQUARE_URL+at,obj,saveUrl,seeUrl); |
|
493 |
if(SimpleTool.checkNotNull(codeUrl)){ |
|
494 |
if(!codeUrl.startsWith("/")){ |
|
495 |
codeUrl = "/"+codeUrl; |
|
496 |
} |
|
497 |
} |
|
498 |
return codeUrl; |
|
499 |
} |
|
500 |
|
|
501 |
public static String twoCodeImgSquare(String at, String path,Integer width, String fileName, String keyId, |
|
502 |
String keySecret, String endPoint, String bucket) throws Exception { |
|
503 |
|
|
504 |
if(!SimpleTool.checkNotNull(width)) { |
|
505 |
width = 430; |
|
506 |
} |
|
507 |
//生成发送数据 |
|
508 |
JSONObject obj = new JSONObject(); |
|
509 |
obj.put("path", path); |
|
510 |
obj.put("width", width); |
|
511 |
|
|
512 |
String imgUrl = null; |
|
513 |
InputStream in = null; |
|
514 |
HttpURLConnection conn = null; |
|
515 |
|
|
516 |
try { |
|
517 |
|
|
518 |
// 创建url资源 |
|
519 |
URL url = new URL(MAKE_TWOCODE_SQUARE_URL + at); |
|
520 |
// 建立http连接 |
|
521 |
conn = (HttpURLConnection) url.openConnection(); |
|
522 |
// 设置允许输出 |
|
523 |
conn.setDoOutput(true); |
|
524 |
conn.setDoInput(true); |
|
525 |
// 设置不用缓存 |
|
526 |
conn.setUseCaches(false); |
|
527 |
// 设置传递方式 |
|
528 |
conn.setRequestMethod("POST"); |
|
529 |
// 设置维持长连接 |
|
530 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
531 |
// 设置文件字符集: |
|
532 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
533 |
// 设置文件类型: |
|
534 |
conn.setRequestProperty("contentType", "application/json"); |
|
535 |
// 开始连接请求 |
|
536 |
conn.connect(); |
|
537 |
OutputStream out = conn.getOutputStream(); |
|
538 |
// 写入请求的字符串 |
|
539 |
out.write((obj.toString()).getBytes()); |
|
540 |
out.flush(); |
|
541 |
out.close(); |
|
542 |
|
|
543 |
// 请求返回的状态 |
|
544 |
if (conn.getResponseCode() == 200) { |
|
545 |
// 请求返回的数据 |
|
546 |
in = conn.getInputStream(); |
|
547 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
548 |
conn.disconnect(); |
|
549 |
conn = null; |
|
550 |
} |
|
551 |
|
|
552 |
if (in != null) { |
|
553 |
in.close(); |
|
554 |
in = null; |
|
555 |
} |
|
556 |
|
|
557 |
if (conn != null) { |
|
558 |
conn.disconnect(); |
|
559 |
conn = null; |
|
560 |
} |
|
561 |
}catch (Exception e) |
|
562 |
{ |
|
563 |
e.printStackTrace(); |
|
564 |
|
|
565 |
if (in != null) { |
|
566 |
try { |
|
567 |
in.close(); |
|
568 |
}catch (Exception ep) |
|
569 |
{ |
|
570 |
ep.printStackTrace(); |
|
571 |
} |
|
572 |
in = null; |
|
573 |
} |
|
574 |
|
|
575 |
if (conn != null) { |
|
576 |
conn.disconnect(); |
|
577 |
conn = null; |
|
578 |
} |
|
579 |
} |
|
580 |
|
|
581 |
return imgUrl; |
|
582 |
|
|
583 |
|
|
584 |
} |
|
585 |
|
|
586 |
/** 生成二维码的图片 返回图片路径 |
|
587 |
* urlx 执行链接 |
|
588 |
* objx 数据 |
|
589 |
* saveUrl 保存图片路径 |
|
590 |
* seeUrl 显示图片路径 |
|
591 |
* |
|
592 |
* **/ |
|
593 |
public static String HttpURLUtilMakeCodeImg(String urlx,JSONObject objx,String saveUrl,String seeUrl) throws IOException { |
|
594 |
String realpath = null; |
|
595 |
|
|
596 |
// 创建url资源 |
|
597 |
URL url = new URL(urlx); |
|
598 |
// 建立http连接 |
|
599 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
600 |
// 设置允许输出 |
|
601 |
conn.setDoOutput(true); |
|
602 |
|
|
603 |
conn.setDoInput(true); |
|
604 |
|
|
605 |
// 设置不用缓存 |
|
606 |
conn.setUseCaches(false); |
|
607 |
// 设置传递方式 |
|
608 |
conn.setRequestMethod("POST"); |
|
609 |
// 设置维持长连接 |
|
610 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
611 |
// 设置文件字符集: |
|
612 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
613 |
// 转换为字节数组 |
|
614 |
// byte[] data = (objx.toString()).getBytes(); |
|
615 |
// 设置文件长度 |
|
616 |
// conn.setRequestProperty("Content-Length",String.valueOf(data.length)); |
|
617 |
|
|
618 |
// 设置文件类型: |
|
619 |
conn.setRequestProperty("contentType", "application/json"); |
|
620 |
// 开始连接请求 |
|
621 |
conn.connect(); |
|
622 |
OutputStream out = conn.getOutputStream(); |
|
623 |
// 写入请求的字符串 |
|
624 |
out.write((objx.toString()).getBytes()); |
|
625 |
out.flush(); |
|
626 |
out.close(); |
|
627 |
|
|
628 |
// 请求返回的状态 |
|
629 |
if (conn.getResponseCode() == 200) { |
|
630 |
System.out.println("连接成功"); |
|
631 |
// 请求返回的数据 |
|
632 |
InputStream in = conn.getInputStream(); |
|
633 |
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
|
634 |
byte[] temp = new byte[1024]; |
|
635 |
int rc = 0; |
|
636 |
while ((rc = in.read(temp, 0, 100)) > 0) { |
|
637 |
swapStream.write(temp, 0, rc); |
|
638 |
} |
|
639 |
byte[] buffer2 = swapStream.toByteArray(); |
|
640 |
String base64 = Base64.encodeBase64String(buffer2); |
|
641 |
|
|
642 |
//生成名称 |
|
643 |
java.util.Random r=new java.util.Random(); |
|
644 |
StringBuilder str=new StringBuilder();//定义变长字符串 |
|
645 |
for(int i=0;i<8;i++){ |
|
646 |
str.append(r.nextInt(10)); |
|
647 |
} |
|
648 |
Date date = new Date(); |
|
649 |
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
650 |
String imgName = sf.format(date); |
|
651 |
imgName = imgName+str.toString(); |
|
652 |
|
|
653 |
//文件保存位置 |
|
654 |
File saveDir = new File(saveUrl); |
|
655 |
if(!saveDir.exists()){ |
|
656 |
saveDir.mkdir(); |
|
657 |
} |
|
658 |
File file = new File(saveDir+"/"+imgName+".png"); |
|
659 |
FileOutputStream fos = new FileOutputStream(file); |
|
660 |
fos.write(buffer2); |
|
661 |
if(fos!=null){ |
|
662 |
fos.close(); |
|
663 |
} |
|
664 |
if(in!=null){ |
|
665 |
in.close(); |
|
666 |
} |
|
667 |
realpath = seeUrl+"/"+imgName+".png"; |
|
668 |
} else { |
|
669 |
System.out.println("no++"); |
|
670 |
} |
|
671 |
return realpath; |
|
672 |
} |
|
673 |
|
|
674 |
/** 请求http协议 获取信息工具 **/ |
|
675 |
private static JSONObject HttpURLUtil(String url,String data) { |
|
676 |
HttpURLConnection con = null; |
|
677 |
URL u; |
|
678 |
String wxMsgXml; |
|
679 |
JSONObject obj; |
|
680 |
try { |
|
681 |
u = new URL(url); |
|
682 |
con = (HttpURLConnection) u.openConnection(); |
|
683 |
con.setRequestMethod("POST"); |
|
684 |
con.setDoOutput(true); |
|
685 |
con.setDoInput(true); |
|
686 |
con.setUseCaches(false); |
|
687 |
con.setReadTimeout(5000); |
|
688 |
con.setRequestProperty("Charset", "UTF-8"); |
|
689 |
con.setRequestProperty("Content-Type", |
|
690 |
"application/x-www-form-urlencoded"); |
|
691 |
if (data != null) { |
|
692 |
OutputStream os = con.getOutputStream(); |
|
693 |
os.write(data.getBytes("utf-8")); |
|
694 |
} |
|
695 |
if (con.getResponseCode() != 200) |
|
696 |
throw new RuntimeException("请求url失败"); |
|
697 |
// 读取返回内容 |
|
698 |
wxMsgXml = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8); |
|
699 |
// 判断返回参数是否正确/成功 |
|
700 |
obj = JSONObject.fromObject(wxMsgXml); |
|
701 |
// System.out.println("HttpURLUtil:"+wxMsgXml); |
|
702 |
} catch (Exception e) { |
|
703 |
e.printStackTrace(); |
|
704 |
obj = new JSONObject(); |
|
705 |
try { |
|
706 |
obj.put("status", 1); |
|
707 |
obj.put("errMsg", e.getMessage()); |
|
708 |
} catch (JSONException e1) { |
|
709 |
e1.printStackTrace(); |
|
710 |
} |
|
711 |
} finally { |
|
712 |
if (con != null) { |
|
713 |
con.disconnect(); |
|
714 |
} |
|
715 |
} |
|
716 |
return obj; |
|
717 |
} |
|
718 |
|
|
719 |
|
|
720 |
|
|
721 |
/**生成无限二维码,返回临时文 |
|
722 |
* @param at 微信token |
|
723 |
* @param scene 参数,只能32位,最好不要中文 |
|
724 |
* @param page 跳转链接 |
|
725 |
* @param width 宽度 |
|
726 |
* @param autoColor 默认false |
|
727 |
* @param lineColor 默认null |
|
728 |
* @param isHyaline 默认false |
|
729 |
* @return |
|
730 |
*/ |
|
731 |
public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
732 |
JSONObject lineColor, boolean isHyaline, String env_version) throws IOException { |
|
733 |
String imgUrl = null; |
|
734 |
InputStream in = null; |
|
735 |
HttpURLConnection conn = null; |
|
736 |
|
|
737 |
//创建临时文件 |
|
738 |
File file = File.createTempFile("temp", ".jpg"); |
|
739 |
|
|
740 |
try { |
|
741 |
//生成发送数据 |
|
742 |
JSONObject obj = new JSONObject(); |
|
743 |
obj.put("scene", scene); |
|
744 |
obj.put("width", width); |
|
745 |
obj.put("page", page); |
|
746 |
obj.put("auto_color", autoColor); |
|
747 |
obj.put("line_color", lineColor); |
|
748 |
obj.put("is_hyaline", isHyaline); |
|
749 |
obj.put("env_version", env_version); |
|
750 |
//体验版时,可不检查页面是否存在 |
|
751 |
if("trial".equals(env_version)){ |
|
752 |
obj.put("check_path", false); |
|
753 |
} |
|
754 |
// 创建url资源 |
|
755 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
756 |
// 建立http连接 |
|
757 |
conn = (HttpURLConnection) url.openConnection(); |
|
758 |
// 设置允许输出 |
|
759 |
conn.setDoOutput(true); |
|
760 |
conn.setDoInput(true); |
|
761 |
// 设置不用缓存 |
|
762 |
conn.setUseCaches(false); |
|
763 |
// 设置传递方式 |
|
764 |
conn.setRequestMethod("POST"); |
|
765 |
// 设置维持长连接 |
|
766 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
767 |
// 设置文件字符集: |
|
768 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
769 |
// 设置文件类型: |
|
770 |
conn.setRequestProperty("contentType", "application/json"); |
|
771 |
// 开始连接请求 |
|
772 |
conn.connect(); |
|
773 |
OutputStream out = conn.getOutputStream(); |
|
774 |
// 写入请求的字符串 |
|
775 |
out.write((obj.toString()).getBytes()); |
|
776 |
out.flush(); |
|
777 |
out.close(); |
|
778 |
|
|
779 |
// 请求返回的状态 |
|
780 |
if (conn.getResponseCode() == 200) { |
|
781 |
// 请求返回的数据 |
|
782 |
in = conn.getInputStream(); |
|
783 |
//输入到临时文件 |
|
784 |
/*OutputStream os = new FileOutputStream(file); |
|
785 |
int bytesRead = 0; |
|
786 |
byte[] buffer = new byte[8192]; |
|
787 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
788 |
os.write(buffer, 0, bytesRead); |
|
789 |
}*/ |
|
790 |
FileUtils.copyInputStreamToFile(in, file); |
|
791 |
|
|
792 |
conn.disconnect(); |
|
793 |
conn = null; |
|
794 |
} |
|
795 |
|
|
796 |
if (in != null) { |
|
797 |
in.close(); |
|
798 |
in = null; |
|
799 |
} |
|
800 |
|
|
801 |
if (conn != null) { |
|
802 |
conn.disconnect(); |
|
803 |
conn = null; |
|
804 |
} |
|
805 |
}catch (Exception e) { |
|
806 |
e.printStackTrace(); |
|
807 |
if (in != null) { |
|
808 |
try { |
|
809 |
in.close(); |
|
810 |
}catch (Exception ep) { |
|
811 |
ep.printStackTrace(); |
|
812 |
} |
|
813 |
in = null; |
|
814 |
} |
|
815 |
|
|
816 |
if (conn != null) { |
|
817 |
conn.disconnect(); |
|
818 |
conn = null; |
|
819 |
} |
|
820 |
}finally { |
|
821 |
file.deleteOnExit(); |
|
822 |
} |
|
823 |
return file; |
|
824 |
} |
|
825 |
|
|
826 |
/** |
|
827 |
* 获取小程序订阅模板 |
|
828 |
* @param accessToken token |
|
829 |
* **/ |
|
830 |
public static HttpHzResponse appTemplateList(String accessToken) { |
|
831 |
return HttpHzUtil.HttpURLUtilJson(GET_APP_TEMPLATE + accessToken, null,null,null,"GET",null); |
|
832 |
} |
|
833 |
|
|
834 |
/** |
|
835 |
* 公众号订阅模板 |
|
836 |
* @param accessToken token |
|
837 |
* **/ |
|
838 |
public static HttpHzResponse gzhTemplateList(String accessToken) { |
|
839 |
return HttpHzUtil.HttpURLUtilJson(GET_GZH_TEMPLATE+accessToken, null,null,null,"GET",null); |
|
840 |
} |
|
841 |
|
|
842 |
} |