chenjiahe
2022-07-27 91bf932d71f53c1caf75eb72013242f438b82c61
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
package com.hx.mp.util;
 
import com.hx.util.OSSUtil;
import com.hx.util.SimpleTool;
import com.hx.util.StringUtils;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 微信获取权限基本工具类
 * @author ChenJiaHe
 * @Date 2020-07-14
 */
@Component
public class MPWeixinBaseUtil {
 
    // 类型
    private static final String GRANT_TYPE = "client_credential";
 
    /**获取access_token链接*/
    private static final String GETACCESS_TOKENURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=";
    /**通过code获取小程序信息链接*/
    private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?";
    /**发送订阅消息通知链接*/
    private static final String SEND_SUBSCRIBE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
    //生成小程序二维码地址(方形)
    public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
    //生成小程序二维码地址(圆形)
    public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}";
    //生成无限二维码
    public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}";
    //////////////////////////////////////////////////
 
    /** (小程序)通过code换取网页授权access_token/如果是snsapi_base模式的授权,这里就可以拿到openId了 ***/
    public static JSONObject getJscode2session(String appid,String secret,String code) {
 
        return HttpURLUtil(JSCODE2SESSION_URL+"appid="+appid+"&secret="+secret
                +"&grant_type=authorization_code&js_code=" + code,null);
    }
 
    /**发送订阅消息通知**/
    public static JSONObject sendSubscribeMessage(String data,String accessToken){
        return HttpURLUtil(SEND_SUBSCRIBE_MESSAGE+accessToken,data);
    }
 
    /** 获取access_token */
    public static JSONObject getAccessToken(String appid,String secret) throws Exception {
        String access_token;
        // 通过WX接口获取access_token
        JSONObject obj = getApplication_Access_tokenForUrl(appid,secret);
        return obj;
    }
 
    /** 从weixin接口获取Access_token **/
    public static JSONObject getApplication_Access_tokenForUrl(String appid,String secret) {
        return HttpURLUtil(GETACCESS_TOKENURL+GRANT_TYPE +"&appid="+appid +"&secret="+secret,null);
    }
 
    /**生成无限二维码,并上传到OSS*/
    public static String createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor,
                                             JSONObject lineColor, boolean isHyaline, String fileName, String keyId,
                                             String keySecret, String endPoint, String bucket)
    {
        String imgUrl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
 
        try {
            //生成发送数据
            JSONObject obj = new JSONObject();
            obj.put("scene", scene);
            obj.put("width", width);
            obj.put("page", page);
            obj.put("auto_color", autoColor);
            obj.put("line_color", lineColor);
            obj.put("is_hyaline", isHyaline);
 
            // 创建url资源
            URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at));
            // 建立http连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();
 
            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                in = conn.getInputStream();
                imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket);
                conn.disconnect();
                conn = null;
            }
 
            if (in != null) {
                in.close();
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
 
            if (in != null) {
                try {
                    in.close();
                }catch (Exception ep)
                {
                    ep.printStackTrace();
                }
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }
 
        return imgUrl;
    }
 
    /**生成有限二维码,返回临时文
     * @param at 微信token
     * @param page 跳转链接
     * @param width 宽度
     * @param autoColor 默认false
     * @param lineColor 默认null
     * @param isHyaline 默认false
     * @return
     */
    public static File createLimitedQrCode(String at, String page, int width, boolean autoColor,
                                           JSONObject lineColor, boolean isHyaline) {
        String imgUrl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
 
        //创建临时文件
        File file = null;
        try {
            file = File.createTempFile("temp", ".jpg");
            //生成发送数据
            JSONObject obj = new JSONObject();
            obj.put("width", width);
            obj.put("path", page);
            obj.put("auto_color", autoColor);
            obj.put("line_color", lineColor);
            obj.put("is_hyaline", isHyaline);
 
            // 创建url资源
            URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at));
            // 建立http连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();
 
            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                in = conn.getInputStream();
                //输入到临时文件
                /*OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }*/
                FileUtils.copyInputStreamToFile(in, file);
 
                conn.disconnect();
                conn = null;
            }
 
            if (in != null) {
                in.close();
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }catch (Exception e) {
            e.printStackTrace();
            if (in != null) {
                try {
                    in.close();
                }catch (Exception ep) {
                    ep.printStackTrace();
                }
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }finally {
            if(file != null){
                file.deleteOnExit();
            }
        }
        return file;
    }
 
 
    /**生成有限二维码,返回临时文
     * @param at 微信token
     * @param page 跳转链接
     * @param width 宽度
     * @param autoColor 默认false
     * @param lineColor 默认null
     * @param isHyaline 默认false
     * @param envVersion 要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
     * @return
     */
    public static File createLimitedQrCode(String at, String page, int width, boolean autoColor,
                                           JSONObject lineColor, boolean isHyaline,String envVersion) {
        String imgUrl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
 
        //创建临时文件
        File file = null;
        try {
            file = File.createTempFile("temp", ".jpg");
            //生成发送数据
            JSONObject obj = new JSONObject();
            obj.put("width", width);
            obj.put("path", page);
            obj.put("auto_color", autoColor);
            obj.put("line_color", lineColor);
            obj.put("is_hyaline", isHyaline);
            obj.put("env_version", envVersion);
 
            // 创建url资源
            URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at));
            // 建立http连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();
 
            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                in = conn.getInputStream();
                //输入到临时文件
                /*OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }*/
                FileUtils.copyInputStreamToFile(in, file);
 
                conn.disconnect();
                conn = null;
            }
 
            if (in != null) {
                in.close();
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }catch (Exception e) {
            e.printStackTrace();
            if (in != null) {
                try {
                    in.close();
                }catch (Exception ep) {
                    ep.printStackTrace();
                }
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }finally {
            if(file != null){
                file.deleteOnExit();
            }
        }
        return file;
    }
 
    /**生成无限二维码,返回临时文
     * @param at 微信token
     * @param scene 参数,只能32位,最好不要中文
     * @param page 跳转链接
     * @param width 宽度
     * @param autoColor 默认false
     * @param lineColor 默认null
     * @param isHyaline 默认false
     * @return
     */
    public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor,
                                             JSONObject lineColor, boolean isHyaline) throws IOException {
        String imgUrl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
 
        //创建临时文件
        File file = File.createTempFile("temp", ".jpg");
 
        try {
            //生成发送数据
            JSONObject obj = new JSONObject();
            obj.put("scene", scene);
            obj.put("width", width);
            obj.put("page", page);
            obj.put("auto_color", autoColor);
            obj.put("line_color", lineColor);
            obj.put("is_hyaline", isHyaline);
 
            // 创建url资源
            URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at));
            // 建立http连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();
 
            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                in = conn.getInputStream();
                //输入到临时文件
                /*OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }*/
                FileUtils.copyInputStreamToFile(in, file);
 
                conn.disconnect();
                conn = null;
            }
 
            if (in != null) {
                in.close();
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }catch (Exception e) {
            e.printStackTrace();
            if (in != null) {
                try {
                    in.close();
                }catch (Exception ep) {
                    ep.printStackTrace();
                }
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }finally {
            file.deleteOnExit();
        }
        return file;
    }
 
 
    /**生成小程序二维码工具(方形)
     * path 二维码跳转链接
     * width 二维码宽度,默认是430
     * saveUrl 保存图片的全路径
     * seeUrl 显示图片路径
     * appid 小程序配置
     * secret 小程序配置
     * return 二维码显示图片链接
     * @throws Exception
     * */
    public static String twoCodeImgSquare(String at, String path,Integer width,String saveUrl,
                                          String seeUrl,String appid,String secret) throws Exception {
 
        if(!SimpleTool.checkNotNull(width)) {
            width = 430;
        }
        //生成发送数据
        JSONObject obj = new JSONObject();
        obj.put("path", path);
        obj.put("width", width);
 
        //二维码图片路径
        String codeUrl = "";
        codeUrl = HttpURLUtilMakeCodeImg(MAKE_TWOCODE_SQUARE_URL+at,obj,saveUrl,seeUrl);
        if(SimpleTool.checkNotNull(codeUrl)){
            if(!codeUrl.startsWith("/")){
                codeUrl = "/"+codeUrl;
            }
        }
        return codeUrl;
    }
 
    public static String twoCodeImgSquare(String at, String path,Integer width, String fileName, String keyId,
                                          String keySecret, String endPoint, String bucket) throws Exception {
 
        if(!SimpleTool.checkNotNull(width)) {
            width = 430;
        }
        //生成发送数据
        JSONObject obj = new JSONObject();
        obj.put("path", path);
        obj.put("width", width);
 
        String imgUrl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
 
        try {
 
            // 创建url资源
            URL url = new URL(MAKE_TWOCODE_SQUARE_URL + at);
            // 建立http连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();
 
            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                in = conn.getInputStream();
                imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket);
                conn.disconnect();
                conn = null;
            }
 
            if (in != null) {
                in.close();
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
 
            if (in != null) {
                try {
                    in.close();
                }catch (Exception ep)
                {
                    ep.printStackTrace();
                }
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }
 
        return imgUrl;
 
 
    }
 
    /** 生成二维码的图片 返回图片路径
     * urlx 执行链接
     * objx 数据
     * saveUrl 保存图片路径
     * seeUrl 显示图片路径
     *
     *  **/
    public static String HttpURLUtilMakeCodeImg(String urlx,JSONObject objx,String saveUrl,String seeUrl) throws IOException {
        String realpath = null;
 
        // 创建url资源
        URL url = new URL(urlx);
        // 建立http连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 设置允许输出
        conn.setDoOutput(true);
 
        conn.setDoInput(true);
 
        // 设置不用缓存
        conn.setUseCaches(false);
        // 设置传递方式
        conn.setRequestMethod("POST");
        // 设置维持长连接
        conn.setRequestProperty("Connection", "Keep-Alive");
        // 设置文件字符集:
        conn.setRequestProperty("Charset", "UTF-8");
        // 转换为字节数组
        // byte[] data = (objx.toString()).getBytes();
        // 设置文件长度
        // conn.setRequestProperty("Content-Length",String.valueOf(data.length));
 
        // 设置文件类型:
        conn.setRequestProperty("contentType", "application/json");
        // 开始连接请求
        conn.connect();
        OutputStream out = conn.getOutputStream();
        // 写入请求的字符串
        out.write((objx.toString()).getBytes());
        out.flush();
        out.close();
 
        // 请求返回的状态
        if (conn.getResponseCode() == 200) {
            System.out.println("连接成功");
            // 请求返回的数据
            InputStream in = conn.getInputStream();
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            int rc = 0;
            while ((rc = in.read(temp, 0, 100)) > 0) {
                swapStream.write(temp, 0, rc);
            }
            byte[] buffer2 = swapStream.toByteArray();
            String base64 = Base64.encodeBase64String(buffer2);
 
            //生成名称
            java.util.Random r=new java.util.Random();
            StringBuilder str=new StringBuilder();//定义变长字符串
            for(int i=0;i<8;i++){
                str.append(r.nextInt(10));
            }
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
            String imgName = sf.format(date);
            imgName = imgName+str.toString();
 
            //文件保存位置
            File saveDir = new File(saveUrl);
            if(!saveDir.exists()){
                saveDir.mkdir();
            }
            File file = new File(saveDir+"/"+imgName+".png");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(buffer2);
            if(fos!=null){
                fos.close();
            }
            if(in!=null){
                in.close();
            }
            realpath = seeUrl+"/"+imgName+".png";
        } else {
            System.out.println("no++");
        }
        return realpath;
    }
 
    /** 请求http协议 获取信息工具 **/
    private static JSONObject HttpURLUtil(String url,String data) {
        HttpURLConnection con = null;
        URL u;
        String wxMsgXml;
        JSONObject obj;
        try {
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setReadTimeout(5000);
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            if (data != null) {
                OutputStream os = con.getOutputStream();
                os.write(data.getBytes("utf-8"));
            }
            if (con.getResponseCode() != 200)
                throw new RuntimeException("请求url失败");
            // 读取返回内容
            wxMsgXml = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8);
            // 判断返回参数是否正确/成功
            obj = JSONObject.fromObject(wxMsgXml);
            // System.out.println("HttpURLUtil:"+wxMsgXml);
        } catch (Exception e) {
            e.printStackTrace();
            obj = new JSONObject();
            try {
                obj.put("status", 1);
                obj.put("errMsg", e.getMessage());
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return obj;
    }
 
 
 
    /**生成无限二维码,返回临时文
     * @param at 微信token
     * @param scene 参数,只能32位,最好不要中文
     * @param page 跳转链接
     * @param width 宽度
     * @param autoColor 默认false
     * @param lineColor 默认null
     * @param isHyaline 默认false
     * @return
     */
    public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor,
                                           JSONObject lineColor, boolean isHyaline, String env_version) throws IOException {
        String imgUrl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
 
        //创建临时文件
        File file = File.createTempFile("temp", ".jpg");
 
        try {
            //生成发送数据
            JSONObject obj = new JSONObject();
            obj.put("scene", scene);
            obj.put("width", width);
            obj.put("page", page);
            obj.put("auto_color", autoColor);
            obj.put("line_color", lineColor);
            obj.put("is_hyaline", isHyaline);
            obj.put("env_version", env_version);
            // 创建url资源
            URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at));
            // 建立http连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();
 
            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                in = conn.getInputStream();
                //输入到临时文件
                /*OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }*/
                FileUtils.copyInputStreamToFile(in, file);
 
                conn.disconnect();
                conn = null;
            }
 
            if (in != null) {
                in.close();
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }catch (Exception e) {
            e.printStackTrace();
            if (in != null) {
                try {
                    in.close();
                }catch (Exception ep) {
                    ep.printStackTrace();
                }
                in = null;
            }
 
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }finally {
            file.deleteOnExit();
        }
        return file;
    }
 
}