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