提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import com.hx.util.OSSUtil; |
|
4 |
import com.hx.util.SimpleTool; |
|
5 |
import com.hx.util.StringUtils; |
|
6 |
import net.sf.json.JSONException; |
|
7 |
import net.sf.json.JSONObject; |
|
8 |
import org.apache.commons.codec.binary.Base64; |
486aaa
|
9 |
import org.apache.commons.io.FileUtils; |
5c5945
|
10 |
import org.apache.commons.io.IOUtils; |
E |
11 |
import org.springframework.stereotype.Component; |
|
12 |
|
|
13 |
import java.io.*; |
|
14 |
import java.net.HttpURLConnection; |
|
15 |
import java.net.URL; |
|
16 |
import java.nio.charset.StandardCharsets; |
|
17 |
import java.text.SimpleDateFormat; |
|
18 |
import java.util.Date; |
|
19 |
|
|
20 |
/** |
|
21 |
* 微信获取权限基本工具类 |
|
22 |
* @author ChenJiaHe |
|
23 |
* @Date 2020-07-14 |
|
24 |
*/ |
|
25 |
@Component |
|
26 |
public class MPWeixinBaseUtil { |
|
27 |
|
|
28 |
// 类型 |
|
29 |
private static final String GRANT_TYPE = "client_credential"; |
|
30 |
|
|
31 |
/**获取access_token链接*/ |
|
32 |
private static final String GETACCESS_TOKENURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type="; |
|
33 |
/**通过code获取小程序信息链接*/ |
|
34 |
private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?"; |
|
35 |
/**发送订阅消息通知链接*/ |
|
36 |
private static final String SEND_SUBSCRIBE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; |
|
37 |
//生成小程序二维码地址(方形) |
|
38 |
public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; |
|
39 |
//生成小程序二维码地址(圆形) |
38fc97
|
40 |
public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}"; |
5c5945
|
41 |
//生成无限二维码 |
E |
42 |
public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; |
|
43 |
////////////////////////////////////////////////// |
|
44 |
|
|
45 |
/** (小程序)通过code换取网页授权access_token/如果是snsapi_base模式的授权,这里就可以拿到openId了 ***/ |
|
46 |
public static JSONObject getJscode2session(String appid,String secret,String code) { |
|
47 |
|
|
48 |
return HttpURLUtil(JSCODE2SESSION_URL+"appid="+appid+"&secret="+secret |
|
49 |
+"&grant_type=authorization_code&js_code=" + code,null); |
|
50 |
} |
|
51 |
|
|
52 |
/**发送订阅消息通知**/ |
|
53 |
public static JSONObject sendSubscribeMessage(String data,String accessToken){ |
|
54 |
return HttpURLUtil(SEND_SUBSCRIBE_MESSAGE+accessToken,data); |
|
55 |
} |
|
56 |
|
|
57 |
/** 获取access_token */ |
|
58 |
public static JSONObject getAccessToken(String appid,String secret) throws Exception { |
|
59 |
String access_token; |
|
60 |
// 通过WX接口获取access_token |
|
61 |
JSONObject obj = getApplication_Access_tokenForUrl(appid,secret); |
|
62 |
return obj; |
|
63 |
} |
|
64 |
|
|
65 |
/** 从weixin接口获取Access_token **/ |
|
66 |
public static JSONObject getApplication_Access_tokenForUrl(String appid,String secret) { |
|
67 |
return HttpURLUtil(GETACCESS_TOKENURL+GRANT_TYPE +"&appid="+appid +"&secret="+secret,null); |
|
68 |
} |
|
69 |
|
|
70 |
/**生成无限二维码,并上传到OSS*/ |
|
71 |
public static String createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
72 |
JSONObject lineColor, boolean isHyaline, String fileName, String keyId, |
|
73 |
String keySecret, String endPoint, String bucket) |
|
74 |
{ |
|
75 |
String imgUrl = null; |
|
76 |
InputStream in = null; |
|
77 |
HttpURLConnection conn = null; |
|
78 |
|
|
79 |
try { |
|
80 |
//生成发送数据 |
|
81 |
JSONObject obj = new JSONObject(); |
|
82 |
obj.put("scene", scene); |
|
83 |
obj.put("width", width); |
|
84 |
obj.put("page", page); |
|
85 |
obj.put("auto_color", autoColor); |
|
86 |
obj.put("line_color", lineColor); |
|
87 |
obj.put("is_hyaline", isHyaline); |
|
88 |
|
|
89 |
// 创建url资源 |
|
90 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
91 |
// 建立http连接 |
|
92 |
conn = (HttpURLConnection) url.openConnection(); |
|
93 |
// 设置允许输出 |
|
94 |
conn.setDoOutput(true); |
|
95 |
conn.setDoInput(true); |
|
96 |
// 设置不用缓存 |
|
97 |
conn.setUseCaches(false); |
|
98 |
// 设置传递方式 |
|
99 |
conn.setRequestMethod("POST"); |
|
100 |
// 设置维持长连接 |
|
101 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
102 |
// 设置文件字符集: |
|
103 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
104 |
// 设置文件类型: |
|
105 |
conn.setRequestProperty("contentType", "application/json"); |
|
106 |
// 开始连接请求 |
|
107 |
conn.connect(); |
|
108 |
OutputStream out = conn.getOutputStream(); |
|
109 |
// 写入请求的字符串 |
|
110 |
out.write((obj.toString()).getBytes()); |
|
111 |
out.flush(); |
|
112 |
out.close(); |
|
113 |
|
|
114 |
// 请求返回的状态 |
|
115 |
if (conn.getResponseCode() == 200) { |
|
116 |
// 请求返回的数据 |
|
117 |
in = conn.getInputStream(); |
|
118 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
119 |
conn.disconnect(); |
|
120 |
conn = null; |
|
121 |
} |
|
122 |
|
|
123 |
if (in != null) { |
|
124 |
in.close(); |
|
125 |
in = null; |
|
126 |
} |
|
127 |
|
|
128 |
if (conn != null) { |
|
129 |
conn.disconnect(); |
|
130 |
conn = null; |
|
131 |
} |
|
132 |
}catch (Exception e) |
|
133 |
{ |
|
134 |
e.printStackTrace(); |
|
135 |
|
|
136 |
if (in != null) { |
|
137 |
try { |
|
138 |
in.close(); |
|
139 |
}catch (Exception ep) |
|
140 |
{ |
|
141 |
ep.printStackTrace(); |
|
142 |
} |
|
143 |
in = null; |
|
144 |
} |
|
145 |
|
|
146 |
if (conn != null) { |
|
147 |
conn.disconnect(); |
|
148 |
conn = null; |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
return imgUrl; |
|
153 |
} |
|
154 |
|
cc9051
|
155 |
/**生成有限二维码,返回临时文 |
C |
156 |
* @param at 微信token |
|
157 |
* @param page 跳转链接 |
|
158 |
* @param width 宽度 |
|
159 |
* @param autoColor 默认false |
|
160 |
* @param lineColor 默认null |
|
161 |
* @param isHyaline 默认false |
|
162 |
* @return |
|
163 |
*/ |
|
164 |
public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, |
fd3fb9
|
165 |
JSONObject lineColor, boolean isHyaline) { |
cc9051
|
166 |
String imgUrl = null; |
C |
167 |
InputStream in = null; |
|
168 |
HttpURLConnection conn = null; |
|
169 |
|
|
170 |
//创建临时文件 |
fd3fb9
|
171 |
File file = null; |
cc9051
|
172 |
try { |
fd3fb9
|
173 |
file = File.createTempFile("temp", ".jpg"); |
cc9051
|
174 |
//生成发送数据 |
C |
175 |
JSONObject obj = new JSONObject(); |
|
176 |
obj.put("width", width); |
38fc97
|
177 |
obj.put("path", page); |
cc9051
|
178 |
obj.put("auto_color", autoColor); |
C |
179 |
obj.put("line_color", lineColor); |
|
180 |
obj.put("is_hyaline", isHyaline); |
|
181 |
|
|
182 |
// 创建url资源 |
|
183 |
URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); |
|
184 |
// 建立http连接 |
|
185 |
conn = (HttpURLConnection) url.openConnection(); |
|
186 |
// 设置允许输出 |
|
187 |
conn.setDoOutput(true); |
|
188 |
conn.setDoInput(true); |
|
189 |
// 设置不用缓存 |
|
190 |
conn.setUseCaches(false); |
|
191 |
// 设置传递方式 |
|
192 |
conn.setRequestMethod("POST"); |
|
193 |
// 设置维持长连接 |
|
194 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
195 |
// 设置文件字符集: |
|
196 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
197 |
// 设置文件类型: |
|
198 |
conn.setRequestProperty("contentType", "application/json"); |
|
199 |
// 开始连接请求 |
|
200 |
conn.connect(); |
|
201 |
OutputStream out = conn.getOutputStream(); |
|
202 |
// 写入请求的字符串 |
|
203 |
out.write((obj.toString()).getBytes()); |
|
204 |
out.flush(); |
|
205 |
out.close(); |
|
206 |
|
|
207 |
// 请求返回的状态 |
|
208 |
if (conn.getResponseCode() == 200) { |
|
209 |
// 请求返回的数据 |
|
210 |
in = conn.getInputStream(); |
|
211 |
//输入到临时文件 |
|
212 |
/*OutputStream os = new FileOutputStream(file); |
|
213 |
int bytesRead = 0; |
|
214 |
byte[] buffer = new byte[8192]; |
|
215 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
216 |
os.write(buffer, 0, bytesRead); |
|
217 |
}*/ |
|
218 |
FileUtils.copyInputStreamToFile(in, file); |
|
219 |
|
|
220 |
conn.disconnect(); |
|
221 |
conn = null; |
|
222 |
} |
|
223 |
|
|
224 |
if (in != null) { |
|
225 |
in.close(); |
|
226 |
in = null; |
|
227 |
} |
|
228 |
|
|
229 |
if (conn != null) { |
|
230 |
conn.disconnect(); |
|
231 |
conn = null; |
|
232 |
} |
|
233 |
}catch (Exception e) { |
|
234 |
e.printStackTrace(); |
|
235 |
if (in != null) { |
|
236 |
try { |
|
237 |
in.close(); |
|
238 |
}catch (Exception ep) { |
|
239 |
ep.printStackTrace(); |
|
240 |
} |
|
241 |
in = null; |
|
242 |
} |
|
243 |
|
|
244 |
if (conn != null) { |
|
245 |
conn.disconnect(); |
|
246 |
conn = null; |
|
247 |
} |
|
248 |
}finally { |
fd3fb9
|
249 |
if(file != null){ |
C |
250 |
file.deleteOnExit(); |
|
251 |
} |
cc9051
|
252 |
} |
C |
253 |
return file; |
|
254 |
} |
|
255 |
|
486aaa
|
256 |
/**生成无限二维码,返回临时文 |
C |
257 |
* @param at 微信token |
|
258 |
* @param scene 参数,只能32位,最好不要中文 |
|
259 |
* @param page 跳转链接 |
|
260 |
* @param width 宽度 |
|
261 |
* @param autoColor 默认false |
|
262 |
* @param lineColor 默认null |
|
263 |
* @param isHyaline 默认false |
|
264 |
* @return |
|
265 |
*/ |
|
266 |
public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
267 |
JSONObject lineColor, boolean isHyaline) throws IOException { |
|
268 |
String imgUrl = null; |
|
269 |
InputStream in = null; |
|
270 |
HttpURLConnection conn = null; |
|
271 |
|
|
272 |
//创建临时文件 |
|
273 |
File file = File.createTempFile("temp", ".jpg"); |
|
274 |
|
|
275 |
try { |
|
276 |
//生成发送数据 |
|
277 |
JSONObject obj = new JSONObject(); |
|
278 |
obj.put("scene", scene); |
|
279 |
obj.put("width", width); |
|
280 |
obj.put("page", page); |
|
281 |
obj.put("auto_color", autoColor); |
|
282 |
obj.put("line_color", lineColor); |
|
283 |
obj.put("is_hyaline", isHyaline); |
|
284 |
|
|
285 |
// 创建url资源 |
|
286 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
287 |
// 建立http连接 |
|
288 |
conn = (HttpURLConnection) url.openConnection(); |
|
289 |
// 设置允许输出 |
|
290 |
conn.setDoOutput(true); |
|
291 |
conn.setDoInput(true); |
|
292 |
// 设置不用缓存 |
|
293 |
conn.setUseCaches(false); |
|
294 |
// 设置传递方式 |
|
295 |
conn.setRequestMethod("POST"); |
|
296 |
// 设置维持长连接 |
|
297 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
298 |
// 设置文件字符集: |
|
299 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
300 |
// 设置文件类型: |
|
301 |
conn.setRequestProperty("contentType", "application/json"); |
|
302 |
// 开始连接请求 |
|
303 |
conn.connect(); |
|
304 |
OutputStream out = conn.getOutputStream(); |
|
305 |
// 写入请求的字符串 |
|
306 |
out.write((obj.toString()).getBytes()); |
|
307 |
out.flush(); |
|
308 |
out.close(); |
|
309 |
|
|
310 |
// 请求返回的状态 |
|
311 |
if (conn.getResponseCode() == 200) { |
|
312 |
// 请求返回的数据 |
|
313 |
in = conn.getInputStream(); |
|
314 |
//输入到临时文件 |
|
315 |
/*OutputStream os = new FileOutputStream(file); |
|
316 |
int bytesRead = 0; |
|
317 |
byte[] buffer = new byte[8192]; |
|
318 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
319 |
os.write(buffer, 0, bytesRead); |
|
320 |
}*/ |
|
321 |
FileUtils.copyInputStreamToFile(in, file); |
|
322 |
|
|
323 |
conn.disconnect(); |
|
324 |
conn = null; |
|
325 |
} |
|
326 |
|
|
327 |
if (in != null) { |
|
328 |
in.close(); |
|
329 |
in = null; |
|
330 |
} |
|
331 |
|
|
332 |
if (conn != null) { |
|
333 |
conn.disconnect(); |
|
334 |
conn = null; |
|
335 |
} |
|
336 |
}catch (Exception e) { |
|
337 |
e.printStackTrace(); |
|
338 |
if (in != null) { |
|
339 |
try { |
|
340 |
in.close(); |
|
341 |
}catch (Exception ep) { |
|
342 |
ep.printStackTrace(); |
|
343 |
} |
|
344 |
in = null; |
|
345 |
} |
|
346 |
|
|
347 |
if (conn != null) { |
|
348 |
conn.disconnect(); |
|
349 |
conn = null; |
|
350 |
} |
|
351 |
}finally { |
|
352 |
file.deleteOnExit(); |
|
353 |
} |
|
354 |
return file; |
|
355 |
} |
|
356 |
|
|
357 |
|
5c5945
|
358 |
/**生成小程序二维码工具(方形) |
E |
359 |
* path 二维码跳转链接 |
|
360 |
* width 二维码宽度,默认是430 |
|
361 |
* saveUrl 保存图片的全路径 |
|
362 |
* seeUrl 显示图片路径 |
|
363 |
* appid 小程序配置 |
|
364 |
* secret 小程序配置 |
|
365 |
* return 二维码显示图片链接 |
|
366 |
* @throws Exception |
|
367 |
* */ |
|
368 |
public static String twoCodeImgSquare(String at, String path,Integer width,String saveUrl, |
|
369 |
String seeUrl,String appid,String secret) throws Exception { |
|
370 |
|
|
371 |
if(!SimpleTool.checkNotNull(width)) { |
|
372 |
width = 430; |
|
373 |
} |
|
374 |
//生成发送数据 |
|
375 |
JSONObject obj = new JSONObject(); |
|
376 |
obj.put("path", path); |
|
377 |
obj.put("width", width); |
|
378 |
|
|
379 |
//二维码图片路径 |
|
380 |
String codeUrl = ""; |
|
381 |
codeUrl = HttpURLUtilMakeCodeImg(MAKE_TWOCODE_SQUARE_URL+at,obj,saveUrl,seeUrl); |
|
382 |
if(SimpleTool.checkNotNull(codeUrl)){ |
|
383 |
if(!codeUrl.startsWith("/")){ |
|
384 |
codeUrl = "/"+codeUrl; |
|
385 |
} |
|
386 |
} |
|
387 |
return codeUrl; |
|
388 |
} |
|
389 |
|
|
390 |
public static String twoCodeImgSquare(String at, String path,Integer width, String fileName, String keyId, |
|
391 |
String keySecret, String endPoint, String bucket) throws Exception { |
|
392 |
|
|
393 |
if(!SimpleTool.checkNotNull(width)) { |
|
394 |
width = 430; |
|
395 |
} |
|
396 |
//生成发送数据 |
|
397 |
JSONObject obj = new JSONObject(); |
|
398 |
obj.put("path", path); |
|
399 |
obj.put("width", width); |
|
400 |
|
|
401 |
String imgUrl = null; |
|
402 |
InputStream in = null; |
|
403 |
HttpURLConnection conn = null; |
|
404 |
|
|
405 |
try { |
|
406 |
|
|
407 |
// 创建url资源 |
|
408 |
URL url = new URL(MAKE_TWOCODE_SQUARE_URL + at); |
|
409 |
// 建立http连接 |
|
410 |
conn = (HttpURLConnection) url.openConnection(); |
|
411 |
// 设置允许输出 |
|
412 |
conn.setDoOutput(true); |
|
413 |
conn.setDoInput(true); |
|
414 |
// 设置不用缓存 |
|
415 |
conn.setUseCaches(false); |
|
416 |
// 设置传递方式 |
|
417 |
conn.setRequestMethod("POST"); |
|
418 |
// 设置维持长连接 |
|
419 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
420 |
// 设置文件字符集: |
|
421 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
422 |
// 设置文件类型: |
|
423 |
conn.setRequestProperty("contentType", "application/json"); |
|
424 |
// 开始连接请求 |
|
425 |
conn.connect(); |
|
426 |
OutputStream out = conn.getOutputStream(); |
|
427 |
// 写入请求的字符串 |
|
428 |
out.write((obj.toString()).getBytes()); |
|
429 |
out.flush(); |
|
430 |
out.close(); |
|
431 |
|
|
432 |
// 请求返回的状态 |
|
433 |
if (conn.getResponseCode() == 200) { |
|
434 |
// 请求返回的数据 |
|
435 |
in = conn.getInputStream(); |
|
436 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
437 |
conn.disconnect(); |
|
438 |
conn = null; |
|
439 |
} |
|
440 |
|
|
441 |
if (in != null) { |
|
442 |
in.close(); |
|
443 |
in = null; |
|
444 |
} |
|
445 |
|
|
446 |
if (conn != null) { |
|
447 |
conn.disconnect(); |
|
448 |
conn = null; |
|
449 |
} |
|
450 |
}catch (Exception e) |
|
451 |
{ |
|
452 |
e.printStackTrace(); |
|
453 |
|
|
454 |
if (in != null) { |
|
455 |
try { |
|
456 |
in.close(); |
|
457 |
}catch (Exception ep) |
|
458 |
{ |
|
459 |
ep.printStackTrace(); |
|
460 |
} |
|
461 |
in = null; |
|
462 |
} |
|
463 |
|
|
464 |
if (conn != null) { |
|
465 |
conn.disconnect(); |
|
466 |
conn = null; |
|
467 |
} |
|
468 |
} |
|
469 |
|
|
470 |
return imgUrl; |
|
471 |
|
|
472 |
|
|
473 |
} |
|
474 |
|
|
475 |
/** 生成二维码的图片 返回图片路径 |
|
476 |
* urlx 执行链接 |
|
477 |
* objx 数据 |
|
478 |
* saveUrl 保存图片路径 |
|
479 |
* seeUrl 显示图片路径 |
|
480 |
* |
|
481 |
* **/ |
|
482 |
public static String HttpURLUtilMakeCodeImg(String urlx,JSONObject objx,String saveUrl,String seeUrl) throws IOException { |
|
483 |
String realpath = null; |
|
484 |
|
|
485 |
// 创建url资源 |
|
486 |
URL url = new URL(urlx); |
|
487 |
// 建立http连接 |
|
488 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
489 |
// 设置允许输出 |
|
490 |
conn.setDoOutput(true); |
|
491 |
|
|
492 |
conn.setDoInput(true); |
|
493 |
|
|
494 |
// 设置不用缓存 |
|
495 |
conn.setUseCaches(false); |
|
496 |
// 设置传递方式 |
|
497 |
conn.setRequestMethod("POST"); |
|
498 |
// 设置维持长连接 |
|
499 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
500 |
// 设置文件字符集: |
|
501 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
502 |
// 转换为字节数组 |
|
503 |
// byte[] data = (objx.toString()).getBytes(); |
|
504 |
// 设置文件长度 |
|
505 |
// conn.setRequestProperty("Content-Length",String.valueOf(data.length)); |
|
506 |
|
|
507 |
// 设置文件类型: |
|
508 |
conn.setRequestProperty("contentType", "application/json"); |
|
509 |
// 开始连接请求 |
|
510 |
conn.connect(); |
|
511 |
OutputStream out = conn.getOutputStream(); |
|
512 |
// 写入请求的字符串 |
|
513 |
out.write((objx.toString()).getBytes()); |
|
514 |
out.flush(); |
|
515 |
out.close(); |
|
516 |
|
|
517 |
// 请求返回的状态 |
|
518 |
if (conn.getResponseCode() == 200) { |
|
519 |
System.out.println("连接成功"); |
|
520 |
// 请求返回的数据 |
|
521 |
InputStream in = conn.getInputStream(); |
|
522 |
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
|
523 |
byte[] temp = new byte[1024]; |
|
524 |
int rc = 0; |
|
525 |
while ((rc = in.read(temp, 0, 100)) > 0) { |
|
526 |
swapStream.write(temp, 0, rc); |
|
527 |
} |
|
528 |
byte[] buffer2 = swapStream.toByteArray(); |
|
529 |
String base64 = Base64.encodeBase64String(buffer2); |
|
530 |
|
|
531 |
//生成名称 |
|
532 |
java.util.Random r=new java.util.Random(); |
|
533 |
StringBuilder str=new StringBuilder();//定义变长字符串 |
|
534 |
for(int i=0;i<8;i++){ |
|
535 |
str.append(r.nextInt(10)); |
|
536 |
} |
|
537 |
Date date = new Date(); |
|
538 |
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
539 |
String imgName = sf.format(date); |
|
540 |
imgName = imgName+str.toString(); |
|
541 |
|
|
542 |
//文件保存位置 |
|
543 |
File saveDir = new File(saveUrl); |
|
544 |
if(!saveDir.exists()){ |
|
545 |
saveDir.mkdir(); |
|
546 |
} |
|
547 |
File file = new File(saveDir+"/"+imgName+".png"); |
|
548 |
FileOutputStream fos = new FileOutputStream(file); |
|
549 |
fos.write(buffer2); |
|
550 |
if(fos!=null){ |
|
551 |
fos.close(); |
|
552 |
} |
|
553 |
if(in!=null){ |
|
554 |
in.close(); |
|
555 |
} |
|
556 |
realpath = seeUrl+"/"+imgName+".png"; |
|
557 |
} else { |
|
558 |
System.out.println("no++"); |
|
559 |
} |
|
560 |
return realpath; |
|
561 |
} |
|
562 |
|
|
563 |
/** 请求http协议 获取信息工具 **/ |
|
564 |
private static JSONObject HttpURLUtil(String url,String data) { |
|
565 |
HttpURLConnection con = null; |
|
566 |
URL u; |
|
567 |
String wxMsgXml; |
|
568 |
JSONObject obj; |
|
569 |
try { |
|
570 |
u = new URL(url); |
|
571 |
con = (HttpURLConnection) u.openConnection(); |
|
572 |
con.setRequestMethod("POST"); |
|
573 |
con.setDoOutput(true); |
|
574 |
con.setDoInput(true); |
|
575 |
con.setUseCaches(false); |
|
576 |
con.setReadTimeout(5000); |
|
577 |
con.setRequestProperty("Charset", "UTF-8"); |
|
578 |
con.setRequestProperty("Content-Type", |
|
579 |
"application/x-www-form-urlencoded"); |
|
580 |
if (data != null) { |
|
581 |
OutputStream os = con.getOutputStream(); |
|
582 |
os.write(data.getBytes("utf-8")); |
|
583 |
} |
|
584 |
if (con.getResponseCode() != 200) |
|
585 |
throw new RuntimeException("请求url失败"); |
|
586 |
// 读取返回内容 |
|
587 |
wxMsgXml = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8); |
|
588 |
// 判断返回参数是否正确/成功 |
|
589 |
obj = JSONObject.fromObject(wxMsgXml); |
|
590 |
// System.out.println("HttpURLUtil:"+wxMsgXml); |
|
591 |
} catch (Exception e) { |
|
592 |
e.printStackTrace(); |
|
593 |
obj = new JSONObject(); |
|
594 |
try { |
|
595 |
obj.put("status", 1); |
|
596 |
obj.put("errMsg", e.getMessage()); |
|
597 |
} catch (JSONException e1) { |
|
598 |
e1.printStackTrace(); |
|
599 |
} |
|
600 |
} finally { |
|
601 |
if (con != null) { |
|
602 |
con.disconnect(); |
|
603 |
} |
|
604 |
} |
|
605 |
return obj; |
|
606 |
} |
|
607 |
|
|
608 |
} |