提交 | 用户 | 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 |
//生成小程序二维码地址(圆形) |
|
40 |
public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token="; |
|
41 |
//生成无限二维码 |
|
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, |
|
165 |
JSONObject lineColor, boolean isHyaline) throws IOException { |
|
166 |
String imgUrl = null; |
|
167 |
InputStream in = null; |
|
168 |
HttpURLConnection conn = null; |
|
169 |
|
|
170 |
//创建临时文件 |
|
171 |
File file = File.createTempFile("temp", ".jpg"); |
|
172 |
|
|
173 |
try { |
|
174 |
//生成发送数据 |
|
175 |
JSONObject obj = new JSONObject(); |
|
176 |
obj.put("width", width); |
|
177 |
obj.put("page", page); |
|
178 |
obj.put("auto_color", autoColor); |
|
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 { |
|
249 |
file.deleteOnExit(); |
|
250 |
} |
|
251 |
return file; |
|
252 |
} |
|
253 |
|
486aaa
|
254 |
/**生成无限二维码,返回临时文 |
C |
255 |
* @param at 微信token |
|
256 |
* @param scene 参数,只能32位,最好不要中文 |
|
257 |
* @param page 跳转链接 |
|
258 |
* @param width 宽度 |
|
259 |
* @param autoColor 默认false |
|
260 |
* @param lineColor 默认null |
|
261 |
* @param isHyaline 默认false |
|
262 |
* @return |
|
263 |
*/ |
|
264 |
public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
265 |
JSONObject lineColor, boolean isHyaline) throws IOException { |
|
266 |
String imgUrl = null; |
|
267 |
InputStream in = null; |
|
268 |
HttpURLConnection conn = null; |
|
269 |
|
|
270 |
//创建临时文件 |
|
271 |
File file = File.createTempFile("temp", ".jpg"); |
|
272 |
|
|
273 |
try { |
|
274 |
//生成发送数据 |
|
275 |
JSONObject obj = new JSONObject(); |
|
276 |
obj.put("scene", scene); |
|
277 |
obj.put("width", width); |
|
278 |
obj.put("page", page); |
|
279 |
obj.put("auto_color", autoColor); |
|
280 |
obj.put("line_color", lineColor); |
|
281 |
obj.put("is_hyaline", isHyaline); |
|
282 |
|
|
283 |
// 创建url资源 |
|
284 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
285 |
// 建立http连接 |
|
286 |
conn = (HttpURLConnection) url.openConnection(); |
|
287 |
// 设置允许输出 |
|
288 |
conn.setDoOutput(true); |
|
289 |
conn.setDoInput(true); |
|
290 |
// 设置不用缓存 |
|
291 |
conn.setUseCaches(false); |
|
292 |
// 设置传递方式 |
|
293 |
conn.setRequestMethod("POST"); |
|
294 |
// 设置维持长连接 |
|
295 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
296 |
// 设置文件字符集: |
|
297 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
298 |
// 设置文件类型: |
|
299 |
conn.setRequestProperty("contentType", "application/json"); |
|
300 |
// 开始连接请求 |
|
301 |
conn.connect(); |
|
302 |
OutputStream out = conn.getOutputStream(); |
|
303 |
// 写入请求的字符串 |
|
304 |
out.write((obj.toString()).getBytes()); |
|
305 |
out.flush(); |
|
306 |
out.close(); |
|
307 |
|
|
308 |
// 请求返回的状态 |
|
309 |
if (conn.getResponseCode() == 200) { |
|
310 |
// 请求返回的数据 |
|
311 |
in = conn.getInputStream(); |
|
312 |
//输入到临时文件 |
|
313 |
/*OutputStream os = new FileOutputStream(file); |
|
314 |
int bytesRead = 0; |
|
315 |
byte[] buffer = new byte[8192]; |
|
316 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
317 |
os.write(buffer, 0, bytesRead); |
|
318 |
}*/ |
|
319 |
FileUtils.copyInputStreamToFile(in, file); |
|
320 |
|
|
321 |
conn.disconnect(); |
|
322 |
conn = null; |
|
323 |
} |
|
324 |
|
|
325 |
if (in != null) { |
|
326 |
in.close(); |
|
327 |
in = null; |
|
328 |
} |
|
329 |
|
|
330 |
if (conn != null) { |
|
331 |
conn.disconnect(); |
|
332 |
conn = null; |
|
333 |
} |
|
334 |
}catch (Exception e) { |
|
335 |
e.printStackTrace(); |
|
336 |
if (in != null) { |
|
337 |
try { |
|
338 |
in.close(); |
|
339 |
}catch (Exception ep) { |
|
340 |
ep.printStackTrace(); |
|
341 |
} |
|
342 |
in = null; |
|
343 |
} |
|
344 |
|
|
345 |
if (conn != null) { |
|
346 |
conn.disconnect(); |
|
347 |
conn = null; |
|
348 |
} |
|
349 |
}finally { |
|
350 |
file.deleteOnExit(); |
|
351 |
} |
|
352 |
return file; |
|
353 |
} |
|
354 |
|
|
355 |
|
5c5945
|
356 |
/**生成小程序二维码工具(方形) |
E |
357 |
* path 二维码跳转链接 |
|
358 |
* width 二维码宽度,默认是430 |
|
359 |
* saveUrl 保存图片的全路径 |
|
360 |
* seeUrl 显示图片路径 |
|
361 |
* appid 小程序配置 |
|
362 |
* secret 小程序配置 |
|
363 |
* return 二维码显示图片链接 |
|
364 |
* @throws Exception |
|
365 |
* */ |
|
366 |
public static String twoCodeImgSquare(String at, String path,Integer width,String saveUrl, |
|
367 |
String seeUrl,String appid,String secret) throws Exception { |
|
368 |
|
|
369 |
if(!SimpleTool.checkNotNull(width)) { |
|
370 |
width = 430; |
|
371 |
} |
|
372 |
//生成发送数据 |
|
373 |
JSONObject obj = new JSONObject(); |
|
374 |
obj.put("path", path); |
|
375 |
obj.put("width", width); |
|
376 |
|
|
377 |
//二维码图片路径 |
|
378 |
String codeUrl = ""; |
|
379 |
codeUrl = HttpURLUtilMakeCodeImg(MAKE_TWOCODE_SQUARE_URL+at,obj,saveUrl,seeUrl); |
|
380 |
if(SimpleTool.checkNotNull(codeUrl)){ |
|
381 |
if(!codeUrl.startsWith("/")){ |
|
382 |
codeUrl = "/"+codeUrl; |
|
383 |
} |
|
384 |
} |
|
385 |
return codeUrl; |
|
386 |
} |
|
387 |
|
|
388 |
public static String twoCodeImgSquare(String at, String path,Integer width, String fileName, String keyId, |
|
389 |
String keySecret, String endPoint, String bucket) throws Exception { |
|
390 |
|
|
391 |
if(!SimpleTool.checkNotNull(width)) { |
|
392 |
width = 430; |
|
393 |
} |
|
394 |
//生成发送数据 |
|
395 |
JSONObject obj = new JSONObject(); |
|
396 |
obj.put("path", path); |
|
397 |
obj.put("width", width); |
|
398 |
|
|
399 |
String imgUrl = null; |
|
400 |
InputStream in = null; |
|
401 |
HttpURLConnection conn = null; |
|
402 |
|
|
403 |
try { |
|
404 |
|
|
405 |
// 创建url资源 |
|
406 |
URL url = new URL(MAKE_TWOCODE_SQUARE_URL + at); |
|
407 |
// 建立http连接 |
|
408 |
conn = (HttpURLConnection) url.openConnection(); |
|
409 |
// 设置允许输出 |
|
410 |
conn.setDoOutput(true); |
|
411 |
conn.setDoInput(true); |
|
412 |
// 设置不用缓存 |
|
413 |
conn.setUseCaches(false); |
|
414 |
// 设置传递方式 |
|
415 |
conn.setRequestMethod("POST"); |
|
416 |
// 设置维持长连接 |
|
417 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
418 |
// 设置文件字符集: |
|
419 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
420 |
// 设置文件类型: |
|
421 |
conn.setRequestProperty("contentType", "application/json"); |
|
422 |
// 开始连接请求 |
|
423 |
conn.connect(); |
|
424 |
OutputStream out = conn.getOutputStream(); |
|
425 |
// 写入请求的字符串 |
|
426 |
out.write((obj.toString()).getBytes()); |
|
427 |
out.flush(); |
|
428 |
out.close(); |
|
429 |
|
|
430 |
// 请求返回的状态 |
|
431 |
if (conn.getResponseCode() == 200) { |
|
432 |
// 请求返回的数据 |
|
433 |
in = conn.getInputStream(); |
|
434 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
435 |
conn.disconnect(); |
|
436 |
conn = null; |
|
437 |
} |
|
438 |
|
|
439 |
if (in != null) { |
|
440 |
in.close(); |
|
441 |
in = null; |
|
442 |
} |
|
443 |
|
|
444 |
if (conn != null) { |
|
445 |
conn.disconnect(); |
|
446 |
conn = null; |
|
447 |
} |
|
448 |
}catch (Exception e) |
|
449 |
{ |
|
450 |
e.printStackTrace(); |
|
451 |
|
|
452 |
if (in != null) { |
|
453 |
try { |
|
454 |
in.close(); |
|
455 |
}catch (Exception ep) |
|
456 |
{ |
|
457 |
ep.printStackTrace(); |
|
458 |
} |
|
459 |
in = null; |
|
460 |
} |
|
461 |
|
|
462 |
if (conn != null) { |
|
463 |
conn.disconnect(); |
|
464 |
conn = null; |
|
465 |
} |
|
466 |
} |
|
467 |
|
|
468 |
return imgUrl; |
|
469 |
|
|
470 |
|
|
471 |
} |
|
472 |
|
|
473 |
/** 生成二维码的图片 返回图片路径 |
|
474 |
* urlx 执行链接 |
|
475 |
* objx 数据 |
|
476 |
* saveUrl 保存图片路径 |
|
477 |
* seeUrl 显示图片路径 |
|
478 |
* |
|
479 |
* **/ |
|
480 |
public static String HttpURLUtilMakeCodeImg(String urlx,JSONObject objx,String saveUrl,String seeUrl) throws IOException { |
|
481 |
String realpath = null; |
|
482 |
|
|
483 |
// 创建url资源 |
|
484 |
URL url = new URL(urlx); |
|
485 |
// 建立http连接 |
|
486 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
487 |
// 设置允许输出 |
|
488 |
conn.setDoOutput(true); |
|
489 |
|
|
490 |
conn.setDoInput(true); |
|
491 |
|
|
492 |
// 设置不用缓存 |
|
493 |
conn.setUseCaches(false); |
|
494 |
// 设置传递方式 |
|
495 |
conn.setRequestMethod("POST"); |
|
496 |
// 设置维持长连接 |
|
497 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
498 |
// 设置文件字符集: |
|
499 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
500 |
// 转换为字节数组 |
|
501 |
// byte[] data = (objx.toString()).getBytes(); |
|
502 |
// 设置文件长度 |
|
503 |
// conn.setRequestProperty("Content-Length",String.valueOf(data.length)); |
|
504 |
|
|
505 |
// 设置文件类型: |
|
506 |
conn.setRequestProperty("contentType", "application/json"); |
|
507 |
// 开始连接请求 |
|
508 |
conn.connect(); |
|
509 |
OutputStream out = conn.getOutputStream(); |
|
510 |
// 写入请求的字符串 |
|
511 |
out.write((objx.toString()).getBytes()); |
|
512 |
out.flush(); |
|
513 |
out.close(); |
|
514 |
|
|
515 |
// 请求返回的状态 |
|
516 |
if (conn.getResponseCode() == 200) { |
|
517 |
System.out.println("连接成功"); |
|
518 |
// 请求返回的数据 |
|
519 |
InputStream in = conn.getInputStream(); |
|
520 |
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
|
521 |
byte[] temp = new byte[1024]; |
|
522 |
int rc = 0; |
|
523 |
while ((rc = in.read(temp, 0, 100)) > 0) { |
|
524 |
swapStream.write(temp, 0, rc); |
|
525 |
} |
|
526 |
byte[] buffer2 = swapStream.toByteArray(); |
|
527 |
String base64 = Base64.encodeBase64String(buffer2); |
|
528 |
|
|
529 |
//生成名称 |
|
530 |
java.util.Random r=new java.util.Random(); |
|
531 |
StringBuilder str=new StringBuilder();//定义变长字符串 |
|
532 |
for(int i=0;i<8;i++){ |
|
533 |
str.append(r.nextInt(10)); |
|
534 |
} |
|
535 |
Date date = new Date(); |
|
536 |
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
537 |
String imgName = sf.format(date); |
|
538 |
imgName = imgName+str.toString(); |
|
539 |
|
|
540 |
//文件保存位置 |
|
541 |
File saveDir = new File(saveUrl); |
|
542 |
if(!saveDir.exists()){ |
|
543 |
saveDir.mkdir(); |
|
544 |
} |
|
545 |
File file = new File(saveDir+"/"+imgName+".png"); |
|
546 |
FileOutputStream fos = new FileOutputStream(file); |
|
547 |
fos.write(buffer2); |
|
548 |
if(fos!=null){ |
|
549 |
fos.close(); |
|
550 |
} |
|
551 |
if(in!=null){ |
|
552 |
in.close(); |
|
553 |
} |
|
554 |
realpath = seeUrl+"/"+imgName+".png"; |
|
555 |
} else { |
|
556 |
System.out.println("no++"); |
|
557 |
} |
|
558 |
return realpath; |
|
559 |
} |
|
560 |
|
|
561 |
/** 请求http协议 获取信息工具 **/ |
|
562 |
private static JSONObject HttpURLUtil(String url,String data) { |
|
563 |
HttpURLConnection con = null; |
|
564 |
URL u; |
|
565 |
String wxMsgXml; |
|
566 |
JSONObject obj; |
|
567 |
try { |
|
568 |
u = new URL(url); |
|
569 |
con = (HttpURLConnection) u.openConnection(); |
|
570 |
con.setRequestMethod("POST"); |
|
571 |
con.setDoOutput(true); |
|
572 |
con.setDoInput(true); |
|
573 |
con.setUseCaches(false); |
|
574 |
con.setReadTimeout(5000); |
|
575 |
con.setRequestProperty("Charset", "UTF-8"); |
|
576 |
con.setRequestProperty("Content-Type", |
|
577 |
"application/x-www-form-urlencoded"); |
|
578 |
if (data != null) { |
|
579 |
OutputStream os = con.getOutputStream(); |
|
580 |
os.write(data.getBytes("utf-8")); |
|
581 |
} |
|
582 |
if (con.getResponseCode() != 200) |
|
583 |
throw new RuntimeException("请求url失败"); |
|
584 |
// 读取返回内容 |
|
585 |
wxMsgXml = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8); |
|
586 |
// 判断返回参数是否正确/成功 |
|
587 |
obj = JSONObject.fromObject(wxMsgXml); |
|
588 |
// System.out.println("HttpURLUtil:"+wxMsgXml); |
|
589 |
} catch (Exception e) { |
|
590 |
e.printStackTrace(); |
|
591 |
obj = new JSONObject(); |
|
592 |
try { |
|
593 |
obj.put("status", 1); |
|
594 |
obj.put("errMsg", e.getMessage()); |
|
595 |
} catch (JSONException e1) { |
|
596 |
e1.printStackTrace(); |
|
597 |
} |
|
598 |
} finally { |
|
599 |
if (con != null) { |
|
600 |
con.disconnect(); |
|
601 |
} |
|
602 |
} |
|
603 |
return obj; |
|
604 |
} |
|
605 |
|
|
606 |
} |