微信官方提供的生成二维码接口得到的是当前公众号的二维码。

一定说明,这种方法我还没有测试,如果有疑问欢迎在评论区域讨论.

....................

...................

谢谢.



微信官方提供的生成二维码接口得到的是当前公众号的二维码。


目前有2种类型的二维码:
1、临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量,主要用于帐号绑定等不要求二维码永久保存的业务场景
2、永久二维码,是无过期时间的,但数量较少(目前为最多10万个),主要用于适用于帐号绑定、用户来源统计等场景


参考文档详见https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542&token=&lang=zh_CN


获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码,但是得到ticket之前首先获取到access_token。

1.创建access_token的bean

可以不写,因为我是每小时定时获取一次token存储到数据库或者直接存到application

[java]  view plain  copy
 
  1. public class AccessToken {    
  2.     // 获取到的凭证    
  3.     private String token;    
  4.     // 凭证有效时间,单位:秒    
  5.     private int expiresIn;  
  6.       
  7.     public String getToken() {  
  8.         return token;  
  9.     }  
  10.     public void setToken(String token) {  
  11.         this.token = token;  
  12.     }  
  13.     public int getExpiresIn() {  
  14.         return expiresIn;  
  15.     }  
  16.     public void setExpiresIn(int expiresIn) {  
  17.         this.expiresIn = expiresIn;  
  18.     }  
  19.   
  20. }  
2.创建获取accessToken的方法

调用接口时,请登录“微信公众平台-开发-基本配置”提前将服务器IP地址添加到IP白名单中,否则将无法调用成功

[java]  view plain  copy
 
  1. /** 
  2.  * 获取access_token 
  3.  * @param appid 凭证 
  4.  * @param appsecret 密钥 
  5.  * @return 
  6.  */  
  7. public static String getAccessToken(String appid, String appsecret) {  
  8.     String result = HttpRequestUtil.getAccessToken(appid,appsecret);  
  9.     JSONObject jsonObject = JSONObject.fromObject(result);  
  10.     if (null != jsonObject) {  
  11.         try {  
  12.             result = jsonObject.getString("access_token");  
  13.         } catch (JSONException e) {  
  14.            logger.info("获取token失败 errcode:"+jsonObject.getInt("errcode") +",errmsg:"+ jsonObject.getString("errmsg"));                
  15.         }  
  16.     }  
  17.     return result;  
  18. }  
3.获取二维码的Ticket

[java]  view plain  copy
 
  1. // 临时二维码  
  2. private final static String QR_SCENE = "QR_SCENE";  
  3. // 永久二维码  
  4. private final static String QR_LIMIT_SCENE = "QR_LIMIT_SCENE";  
  5. // 永久二维码(字符串)  
  6. private final static String QR_LIMIT_STR_SCENE = "QR_LIMIT_STR_SCENE";   
  7. // 创建二维码  
  8. private String create_ticket_path = "https://api.weixin.qq.com/cgi-bin/qrcode/create";  
  9. // 通过ticket换取二维码  
  10. private String showqrcode_path = "https://mp.weixin.qq.com/cgi-bin/showqrcode";  
  11.   
  12. /** 
  13.  * 创建临时带参数二维码 
  14.  * @param accessToken 
  15.  * @expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。 
  16.  * @param sceneId 场景Id 
  17.  * @return 
  18.  */  
  19. public String createTempTicket(String accessToken, String expireSeconds, int sceneId) {  
  20.     WeiXinQRCode wxQRCode = null;  
  21.       
  22.     TreeMap<String,String> params = new TreeMap<String,String>();  
  23.     params.put("access_token", accessToken);  
  24.     Map<String,Integer> intMap = new HashMap<String,Integer>();  
  25.     intMap.put("scene_id",sceneId);  
  26.     Map<String,Map<String,Integer>> mapMap = new HashMap<String,Map<String,Integer>>();  
  27.     mapMap.put("scene", intMap);  
  28.     //  
  29.     Map<String,Object> paramsMap = new HashMap<String,Object>();  
  30.     paramsMap.put("expire_seconds", expireSeconds);  
  31.     paramsMap.put("action_name", QR_SCENE);  
  32.     paramsMap.put("action_info", mapMap);  
  33.     String data = new Gson().toJson(paramsMap);  
  34.     data = HttpRequestUtil.HttpsDefaultExecute(HttpRequestUtil.POST_METHOD,create_ticket_path,params,data);  
  35.     try {  
  36.         wxQRCode = new Gson().fromJson(data, WeiXinQRCode.class);  
  37.     } catch (JsonSyntaxException e) {  
  38.         wxQRCode = null;  
  39.         e.printStackTrace();  
  40.     }  
  41.     return wxQRCode==null?null:wxQRCode.getTicket();  
  42.   
  43. }  
  44.   
  45. /** 
  46.  * 创建永久二维码(数字) 
  47.  * @param accessToken 
  48.  * @param sceneId 场景Id 
  49.  * @return 
  50.  */  
  51. public String createForeverTicket(String accessToken, int sceneId) {  
  52.       
  53.     TreeMap<String,String> params = new TreeMap<String,String>();  
  54.     params.put("access_token", accessToken);  
  55.     //output data  
  56.     Map<String,Integer> intMap = new HashMap<String,Integer>();  
  57.     intMap.put("scene_id",sceneId);  
  58.     Map<String,Map<String,Integer>> mapMap = new HashMap<String,Map<String,Integer>>();  
  59.     mapMap.put("scene", intMap);  
  60.     //  
  61.     Map<String,Object> paramsMap = new HashMap<String,Object>();  
  62.     paramsMap.put("action_name", QR_LIMIT_SCENE);  
  63.     paramsMap.put("action_info", mapMap);  
  64.     String data = new Gson().toJson(paramsMap);  
  65.     data =  HttpRequestUtil.HttpsDefaultExecute(HttpRequestUtil.POST_METHOD,create_ticket_path,params,data);  
  66.     WeiXinQRCode wxQRCode = null;  
  67.     try {  
  68.         wxQRCode = new Gson().fromJson(data, WeiXinQRCode.class);  
  69.     } catch (JsonSyntaxException e) {  
  70.         wxQRCode = null;  
  71.         e.printStackTrace();  
  72.     }  
  73.     return wxQRCode==null?null:wxQRCode.getTicket();  
  74. }  
  75.   
  76. /** 
  77.  * 创建永久二维码(字符串) 
  78.  *  
  79.  * @param accessToken 
  80.  * @param sceneStr 场景str 
  81.  * @return 
  82.  */  
  83. public String createForeverStrTicket(String accessToken, String sceneStr){        
  84.     TreeMap<String,String> params = new TreeMap<String,String>();  
  85.     params.put("access_token", accessToken);  
  86.     //output data  
  87.     Map<String,String> intMap = new HashMap<String,String>();  
  88.     intMap.put("scene_str",sceneStr);  
  89.     Map<String,Map<String,String>> mapMap = new HashMap<String,Map<String,String>>();  
  90.     mapMap.put("scene", intMap);  
  91.       
  92.     Map<String,Object> paramsMap = new HashMap<String,Object>();  
  93.     paramsMap.put("action_name", QR_LIMIT_STR_SCENE);  
  94.     paramsMap.put("action_info", mapMap);  
  95.     String data = new Gson().toJson(paramsMap);  
  96.     data =  HttpRequestUtil.HttpsDefaultExecute(HttpRequestUtil.POST_METHOD,create_ticket_path,params,data);  
  97.     WeiXinQRCode wxQRCode = null;  
  98.     try {  
  99.         wxQRCode = new Gson().fromJson(data, WeiXinQRCode.class);  
  100.     } catch (JsonSyntaxException e) {  
  101.         wxQRCode = null;  
  102.     }  
  103.     return wxQRCode==null?null:wxQRCode.getTicket();  
  104. }  

强烈建议用测试号生成永久的

4.通过ticket凭证直接获取二维码
[java]  view plain  copy
 
  1. /** 
  2.  * 获取二维码ticket后,通过ticket换取二维码图片展示 
  3.  * @param ticket 
  4.  * @return 
  5.  */  
  6. public String showQrcode(String ticket){  
  7.     Map<String,String> params = new TreeMap<String,String>();  
  8.     params.put("ticket", HttpRequestUtil.urlEncode(ticket, HttpRequestUtil.DEFAULT_CHARSET));  
  9.     try {  
  10.         showqrcode_path = HttpRequestUtil.setParmas(params,showqrcode_path,"");  
  11.     } catch (Exception e) {  
  12.         e.printStackTrace();  
  13.     }  
  14.     return showqrcode_path;  
  15. }  
  16.   
  17. /** 
  18.  * 获取二维码ticket后,通过ticket换取二维码图片 
  19.  * @param ticket 
  20.  * @param savePath  保存的路径,例如 F:\\test\test.jpg 
  21.  * @return      Result.success = true 表示下载图片下载成功 
  22.  */  
  23. public WeiXinResult showQrcode(String ticket,String savePath) throws Exception{  
  24.     TreeMap<String,String> params = new TreeMap<String,String>();  
  25.     params.put("ticket", HttpRequestUtil.urlEncode(ticket, HttpRequestUtil.DEFAULT_CHARSET));  
  26.     WeiXinResult result = HttpRequestUtil.downMeaterMetod(params,HttpRequestUtil.GET_METHOD,showqrcode_path,savePath);  
  27.     return result;  
  28. }  

5.用户扫描带参数的二维码的处理

如果没有关注的话,用户可以关注公众号,并且会这个场景值就是所谓的参数推送给开发者,参数是EventKey,直接用request获取即可,注意qrscene_的之后才是二维码的参数值

如果已经关注,微信会直接把场景值的扫描事件直接推送给开发者


6.参考类

WeiXinResult.Java

[java]  view plain  copy
 
  1. /** 
  2.  * 返回的结果对象 
  3.  * @author fjing 
  4.  *  
  5.  */  
  6. public class WeiXinResult {  
  7.       
  8.     public static final int NEWSMSG = 1;            //图文消息  
  9.     private boolean isSuccess;  
  10.     private Object obj;  
  11.     private int type;  
  12.     private String msg;  
  13.   
  14.     public String getMsg() {  
  15.         return msg;  
  16.     }  
  17.   
  18.     public void setMsg(String msg) {  
  19.         this.msg = msg;  
  20.     }  
  21.   
  22.     public int getType() {  
  23.         return type;  
  24.     }  
  25.   
  26.     public void setType(int type) {  
  27.         this.type = type;  
  28.     }  
  29.   
  30.     public boolean isSuccess() {  
  31.         return isSuccess;  
  32.     }  
  33.   
  34.     public void setSuccess(boolean isSuccess) {  
  35.         this.isSuccess = isSuccess;  
  36.     }  
  37.   
  38.     public Object getObj() {  
  39.         return obj;  
  40.     }  
  41.   
  42.     public void setObj(Object obj) {  
  43.         this.obj = obj;  
  44.     }  
  45. }  

HttpRequestUtil.java

[java]  view plain  copy
 
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.io.UnsupportedEncodingException;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.MalformedURLException;  
  10. import java.net.URL;  
  11. import java.net.URLEncoder;  
  12. import java.util.Map;  
  13. import java.util.Map.Entry;  
  14. import java.util.Set;  
  15. import java.util.TreeMap;  
  16.   
  17. import javax.net.ssl.HostnameVerifier;  
  18. import javax.net.ssl.HttpsURLConnection;  
  19. import javax.net.ssl.KeyManager;  
  20. import javax.net.ssl.SSLContext;  
  21. import javax.net.ssl.SSLSession;  
  22. import javax.net.ssl.TrustManager;  
  23.   
  24. import com.dingshu.model.result.WeiXinResult;  
  25.   
  26. public class HttpRequestUtil {  
  27.   
  28.     //private static Logger logger = Logger.getLogger(HttpRequestUtil.class);  
  29.   
  30.     public static final String GET_METHOD = "GET";  
  31.   
  32.     public static final String POST_METHOD = "POST";  
  33.   
  34.     public static final String DEFAULT_CHARSET = "utf-8";  
  35.   
  36.     private static int DEFAULT_CONNTIME = 5000;  
  37.       
  38.     private static int DEFAULT_READTIME = 5000;  
  39.     // 获取access_token的路径  
  40.     private static String token_path = "https://api.weixin.qq.com/cgi-bin/token";  
  41.   
  42.     /** 
  43.      * http请求 
  44.      *  
  45.      * @param method 
  46.      *            请求方法GET/POST 
  47.      * @param path 
  48.      *            请求路径 
  49.      * @param timeout 
  50.      *            连接超时时间 默认为5000 
  51.      * @param readTimeout 
  52.      *            读取超时时间 默认为5000 
  53.      * @param data  数据 
  54.      * @return 
  55.      */  
  56.     public static String defaultConnection(String method, String path, int timeout, int readTimeout, String data)  
  57.             throws Exception {  
  58.         String result = "";  
  59.         URL url = new URL(path);  
  60.         if (url != null) {  
  61.             HttpURLConnection conn = getConnection(method, url);  
  62.             conn.setConnectTimeout(timeout == 0 ? DEFAULT_CONNTIME : timeout);  
  63.             conn.setReadTimeout(readTimeout == 0 ? DEFAULT_READTIME : readTimeout);  
  64.             if (data != null && !"".equals(data)) {  
  65.                 OutputStream output = conn.getOutputStream();  
  66.                 output.write(data.getBytes(DEFAULT_CHARSET));  
  67.                 output.flush();  
  68.                 output.close();  
  69.             }  
  70.             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  71.                 InputStream input = conn.getInputStream();  
  72.                 result = inputToStr(input);  
  73.                 input.close();  
  74.                 conn.disconnect();  
  75.             }  
  76.         }  
  77.         return result;  
  78.     }  
  79.   
  80.     /** 
  81.      * 根据url的协议选择对应的请求方式 例如 http://www.baidu.com 则使用http请求,https://www.baidu.com 
  82.      * 则使用https请求 
  83.      *  
  84.      * @param method 
  85.      *            请求的方法 
  86.      * @return 
  87.      * @throws IOException 
  88.      */  
  89.     private static HttpURLConnection getConnection(String method, URL url) throws IOException {  
  90.         HttpURLConnection conn = null;  
  91.         if ("https".equals(url.getProtocol())) {  
  92.             SSLContext context = null;  
  93.             try {  
  94.                 context = SSLContext.getInstance("SSL""SunJSSE");  
  95.                 context.init(new KeyManager[0], new TrustManager[] { new MyX509TrustManager() },  
  96.                         new java.security.SecureRandom());  
  97.             } catch (Exception e) {  
  98.                 throw new IOException(e);  
  99.             }  
  100.             HttpsURLConnection connHttps = (HttpsURLConnection) url.openConnection();  
  101.             connHttps.setSSLSocketFactory(context.getSocketFactory());  
  102.             connHttps.setHostnameVerifier(new HostnameVerifier() {  
  103.   
  104.                 @Override  
  105.                 public boolean verify(String arg0, SSLSession arg1) {  
  106.                     return true;  
  107.                 }  
  108.             });  
  109.             conn = connHttps;  
  110.         } else {  
  111.             conn = (HttpURLConnection) url.openConnection();  
  112.         }  
  113.         conn.setRequestMethod(method);  
  114.         conn.setUseCaches(false);  
  115.         conn.setDoInput(true);  
  116.         conn.setDoOutput(true);  
  117.         return conn;  
  118.     }  
  119.   
  120.     /** 
  121.      * 将输入流转换为字符串 
  122.      *  
  123.      * @param input 
  124.      *            输入流 
  125.      * @return 转换后的字符串 
  126.      */  
  127.     public static String inputToStr(InputStream input) {  
  128.         String result = "";  
  129.         if (input != null) {  
  130.             byte[] array = new byte[1024];  
  131.             StringBuffer buffer = new StringBuffer();  
  132.             try {  
  133.                 for (int index; (index = (input.read(array))) > -1;) {  
  134.                     buffer.append(new String(array, 0, index, DEFAULT_CHARSET));  
  135.                 }  
  136.                 result = buffer.toString();  
  137.             } catch (IOException e) {  
  138.                 e.printStackTrace();  
  139.                 result = "";  
  140.             }  
  141.         }  
  142.         return result;  
  143.     }  
  144.   
  145.     /** 
  146.      * 设置参数 
  147.      *  
  148.      * @param map 
  149.      *            参数map 
  150.      * @param path 
  151.      *            需要赋值的path 
  152.      * @param charset 
  153.      *            编码格式 默认编码为utf-8(取消默认) 
  154.      * @return 已经赋值好的url 只需要访问即可 
  155.      */  
  156.     public static String setParmas(Map<String, String> map, String path, String charset) throws Exception {  
  157.         String result = "";  
  158.         boolean hasParams = false;  
  159.         if (path != null && !"".equals(path)) {  
  160.             if (map != null && map.size() > 0) {  
  161.                 StringBuilder builder = new StringBuilder();  
  162.                 Set<Entry<String, String>> params = map.entrySet();  
  163.                 for (Entry<String, String> entry : params) {  
  164.                     String key = entry.getKey().trim();  
  165.                     String value = entry.getValue().trim();  
  166.                     if (hasParams) {  
  167.                         builder.append("&");  
  168.                     } else {  
  169.                         hasParams = true;  
  170.                     }  
  171.                     if(charset != null && !"".equals(charset)){  
  172.                         //builder.append(key).append("=").append(URLDecoder.(value, charset));    
  173.                         builder.append(key).append("=").append(urlEncode(value, charset));  
  174.                     }else{  
  175.                         builder.append(key).append("=").append(value);  
  176.                     }  
  177.                 }  
  178.                 result = builder.toString();  
  179.             }  
  180.         }  
  181.         return doUrlPath(path, result).toString();  
  182.     }  
  183.   
  184.     /** 
  185.      * 设置连接参数 
  186.      *  
  187.      * @param path 
  188.      *            路径 
  189.      * @return 
  190.      */  
  191.     private static URL doUrlPath(String path, String query) throws Exception {  
  192.         URL url = new URL(path);  
  193.         if (org.apache.commons.lang.StringUtils.isEmpty(path)) {  
  194.             return url;  
  195.         }  
  196.         if (org.apache.commons.lang.StringUtils.isEmpty(url.getQuery())) {  
  197.             if (path.endsWith("?")) {  
  198.                 path += query;  
  199.             } else {  
  200.                 path = path + "?" + query;  
  201.             }  
  202.         } else {  
  203.             if (path.endsWith("&")) {  
  204.                 path += query;  
  205.             } else {  
  206.                 path = path + "&" + query;  
  207.             }  
  208.         }  
  209.         return new URL(path);  
  210.     }  
  211.   
  212.     /** 
  213.      * 默认的http请求执行方法,返回 
  214.      *  
  215.      * @param method 
  216.      *            请求的方法 POST/GET 
  217.      * @param path 
  218.      *            请求path 路径 
  219.      * @param map 
  220.      *            请求参数集合 
  221.      * @param data 
  222.      *            输入的数据 允许为空 
  223.      * @return 
  224.      */  
  225.     public static String HttpDefaultExecute(String method, String path, Map<String, String> map, String data) {  
  226.         String result = "";  
  227.         try {  
  228.             String url = setParmas((TreeMap<String, String>) map, path, "");  
  229.             result = defaultConnection(method, url, DEFAULT_CONNTIME, DEFAULT_READTIME, data);  
  230.         } catch (Exception e) {  
  231.             e.printStackTrace();  
  232.         }  
  233.         return result;  
  234.     }  
  235.   
  236.     /** 
  237.      * 默认的https执行方法,返回 
  238.      *  
  239.      * @param method 
  240.      *            请求的方法 POST/GET 
  241.      * @param path 
  242.      *            请求path 路径 
  243.      * @param map 
  244.      *            请求参数集合 
  245.      * @param data 
  246.      *            输入的数据 允许为空 
  247.      * @return 
  248.      */  
  249.     public static String HttpsDefaultExecute(String method, String path, Map<String, String> map, String data) {  
  250.         String result = "";  
  251.         try {  
  252.             String url = setParmas((TreeMap<String, String>) map, path, "");  
  253.             result = defaultConnection(method, url, DEFAULT_CONNTIME, DEFAULT_READTIME, data);  
  254.         } catch (Exception e) {  
  255.             e.printStackTrace();  
  256.         }  
  257.         return result;  
  258.     }  
  259.   
  260.     /** 
  261.      * 获取授权token 
  262.      *  
  263.      * @param key 应用key 
  264.      * @param secret 应用密匙 
  265.      * @return json格式的字符串 
  266.      */  
  267.     public static String getAccessToken(String key, String secret) {  
  268.         TreeMap<String, String> map = new TreeMap<String, String>();  
  269.         map.put("grant_type""client_credential");  
  270.         map.put("appid", key);  
  271.         map.put("secret", secret);  
  272.         String result = HttpDefaultExecute(GET_METHOD, token_path, map, "");  
  273.         return result;  
  274.     }  
  275.   
  276.     /** 
  277.      * 默认的下载素材方法 
  278.      *  
  279.      * @param method  http方法 POST/GET 
  280.      * @param apiPath api路径 
  281.      * @param savePath 素材需要保存的路径 
  282.      * @return 是否下载成功 Reuslt.success==true 表示下载成功 
  283.      */  
  284.     public static WeiXinResult downMeaterMetod(TreeMap<String, String> params, String method, String apiPath, String savePath) {  
  285.         WeiXinResult result = new WeiXinResult();  
  286.         try {  
  287.             apiPath = setParmas(params, apiPath, "");  
  288.             URL url = new URL(apiPath);  
  289.             HttpURLConnection conn = getConnection(method, url);  
  290.             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  291.                 String contentType = conn.getContentType();  
  292.                 result = ContentType(contentType, conn, savePath);  
  293.             } else {  
  294.                 result.setObj(conn.getResponseCode() + " " + conn.getResponseMessage());  
  295.             }  
  296.         } catch (MalformedURLException e) {  
  297.             e.printStackTrace();  
  298.         } catch (IOException e) {  
  299.             e.printStackTrace();  
  300.         } catch (Exception e) {  
  301.             e.printStackTrace();  
  302.         }  
  303.         return result;  
  304.     }  
  305.   
  306.     /** 
  307.      * 根据返回的头信息返回具体信息 
  308.      *  
  309.      * @param contentType 
  310.      *            ContentType请求头信息 
  311.      * @return Result.type==1 表示文本消息, 
  312.      */  
  313.     private static WeiXinResult ContentType(String contentType, HttpURLConnection conn, String savePath) {  
  314.         WeiXinResult result = new WeiXinResult();  
  315.         try {  
  316.             if (conn != null) {  
  317.                 InputStream input = conn.getInputStream();  
  318.                 if (contentType.equals("image/gif")) { // gif图片  
  319.                     result = InputStreamToMedia(input, savePath, "gif");  
  320.                 } else if (contentType.equals("image/jpeg")) { // jpg图片  
  321.                     result = InputStreamToMedia(input, savePath, "jpg");  
  322.                 } else if (contentType.equals("image/jpg")) { // jpg图片  
  323.                     result = InputStreamToMedia(input, savePath, "jpg");  
  324.                 } else if (contentType.equals("image/png")) { // png图片  
  325.                     result = InputStreamToMedia(input, savePath, "png");  
  326.                 } else if (contentType.equals("image/bmp")) { // bmp图片  
  327.                     result = InputStreamToMedia(input, savePath, "bmp");  
  328.                 } else if (contentType.equals("audio/x-wav")) { // wav语音  
  329.                     result = InputStreamToMedia(input, savePath, "wav");  
  330.                 } else if (contentType.equals("audio/x-ms-wma")) { // wma语言  
  331.                     result = InputStreamToMedia(input, savePath, "wma");  
  332.                 } else if (contentType.equals("audio/mpeg")) { // mp3语言  
  333.                     result = InputStreamToMedia(input, savePath, "mp3");  
  334.                 } else if (contentType.equals("text/plain")) { // 文本信息  
  335.                     String str = inputToStr(input);  
  336.                     result.setObj(str);  
  337.                 } else if (contentType.equals("application/json")) { // 返回json格式的数据  
  338.                     String str = inputToStr(input);  
  339.                     result.setObj(str);  
  340.                 } else { // 此处有问题  
  341.                     String str = inputToStr(input);  
  342.                     result.setObj(str);  
  343.                 }  
  344.             } else {  
  345.                 result.setObj("conn is null!");  
  346.             }  
  347.         } catch (Exception ex) {  
  348.             ex.printStackTrace();  
  349.         }  
  350.         return result;  
  351.     }  
  352.   
  353.     /** 
  354.      * 将字符流转换为图片文件 
  355.      *  
  356.      * @param input 字符流 
  357.      * @param savePath 图片需要保存的路径 
  358.      * @param 类型 jpg/png等 
  359.      * @return 
  360.      */  
  361.     private static WeiXinResult InputStreamToMedia(InputStream input, String savePath, String type) {  
  362.         WeiXinResult result = new WeiXinResult();  
  363.         try {  
  364.             File file = null;  
  365.             file = new File(savePath);  
  366.             String paramPath = file.getParent(); // 路径  
  367.             String fileName = file.getName(); //  
  368.             String newName = fileName.substring(0, fileName.lastIndexOf(".")) + "." + type;// 根据实际返回的文件类型后缀  
  369.             savePath = paramPath + "\\" + newName;  
  370.             if (!file.exists()) {  
  371.                 File dirFile = new File(paramPath);  
  372.                 dirFile.mkdirs();  
  373.             }  
  374.             file = new File(savePath);  
  375.             FileOutputStream output = new FileOutputStream(file);  
  376.             int len = 0;  
  377.             byte[] array = new byte[1024];  
  378.             while ((len = input.read(array)) != -1) {  
  379.                 output.write(array, 0, len);  
  380.             }  
  381.             output.flush();  
  382.             output.close();  
  383.             result.setSuccess(true);  
  384.             result.setObj("save success!");  
  385.         } catch (FileNotFoundException e) {  
  386.             e.printStackTrace();  
  387.             result.setSuccess(false);  
  388.             result.setObj(e.getMessage());  
  389.         } catch (IOException e) {  
  390.             e.printStackTrace();  
  391.             result.setSuccess(false);  
  392.             result.setObj(e.getMessage());  
  393.         }  
  394.         return result;  
  395.     }  
  396.   
  397.     public static String urlEncode(String source, String encode) {  
  398.         String result = source;  
  399.         try {  
  400.             result = URLEncoder.encode(source, encode);  
  401.         } catch (UnsupportedEncodingException e) {  
  402.             e.printStackTrace();  
  403.         }  
  404.         return result;  
  405.     }  
  406. }  
信任管理器MyX509TrustManager,实现方法自己定义(反正我没写)

[java]  view plain  copy
 
  1. /** 
  2.  * 信任管理器 
  3.  * @author fjing 
  4.  * 
  5.  */  
  6. public class MyX509TrustManager implements X509TrustManager {  
  7.   
  8.     @Override  
  9.     public void checkClientTrusted(X509Certificate[] arg0, String arg1)  
  10.             throws CertificateException {  
  11.   
  12.     }  
  13.   
  14.     @Override  
  15.     public void checkServerTrusted(X509Certificate[] arg0, String arg1)  
  16.             throws CertificateException {  
  17.   
  18.     }  
  19.   
  20.     @Override  
  21.     public X509Certificate[] getAcceptedIssuers() {  
  22.         return null;  
  23.     }  
  24.   
  25. }  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值