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