Article From:https://www.cnblogs.com/yigedingzi/p/9970861.html

Step 1: Preparations: Preparing APPID and SERET for the applet

  Visit AppID and secret on Wechat Public Platform

Part 2: Getting token

  Wechat provides an interface to get token. Accessing this interface and passing in APPID and secret as parameters, we can get token value.

  The interface address is:tokenURL = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET

  JavaCode:

 1 public static String getAccessToken() throws UnsupportedEncodingException, IOException{
 2         String tokenURL = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET
 3         JSONObject jsonObject = null;
 4         URL conn_url =  new URL(tokenURL );
 5         HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection();
 6         conn.setRequestMethod("GET");
 7         conn.setReadTimeout(5000);
 8         conn.setConnectTimeout(5000);
 9         conn.connect();
10         //outputGetting access_token is not required11        
12         //Normal return code is 200
13         if(conn.getResponseCode()==200){
14             InputStream stream = conn.getInputStream();
15             InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
16             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
17             String str = null;
18             StringBuffer buffer = new StringBuffer();
19             while ((str = bufferedReader.readLine()) != null) {
20                 buffer.append(str);
21             }
22             bufferedReader.close();
23             inputStreamReader.close();
24           stream.close();
25           conn.disconnect();
26             jsonObject = JSONObject.fromObject(buffer.toString());
27         }
28         
29         String access_token = jsonObject.getString("access_token");
30         return access_token;
31     }

Part 3: Getting Parametric Pictures

  The official of Wechat provides an interface for acquiring the two-dimensional code of the Wechat applet, accessing the interface, and passing in the token value obtained in the second step as a parameter. At the same time, the parameters needed in the two-dimensional code and some setting parameters of the picture are also needed to obtain the input stream of the two-dimensional code picture.

  Interface address:

  https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token

  Basic setting parameters:

        ”scene”, param     –Parameter param needed in two-dimensional code
        ”path”, “pages/ShouYe/ShouYe/ShouYe”   –Set up the page to which the two-dimensional scan code enters
        ”width”, width          –Set the size of the two-dimensional code picture. If not, the default size is 430.
        ”is_hyaline”, true
        ”auto_color”, true

 

  Generate two-dimensional code:

  

 1 public static String getQRImg(String param,String size, HttpServletRequest request) {
 2         
 3         
 4         String p=request.getSession().getServletContext().getRealPath("/");
 5         String codeUrl = p+"image/codeImg/"+System.currentTimeMillis()+param + "Code.png";
 6         String twoCodeUrl="/image/codeImg/"+System.currentTimeMillis()+param + "Code.png";
 7          try
 8          {
 9              String access_token = token;
10              URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
11              HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
12              httpURLConnection.setRequestMethod("POST");// Submission mode13              // conn.setConnectTimeout(10000);//Connection timeout unit milliseconds14              // conn.setReadTimeout(2000);//Read timeout unit milliseconds15              // To send a POST request, you must set the following two lines
16              httpURLConnection.setDoOutput(true);
17              httpURLConnection.setDoInput(true);
18              // Get the output stream corresponding to the URLConnection object
19              PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
20              // Send request parameters
21              JSONObject paramJson = new JSONObject();
22              String width;
23              if(size !=null && !size.trim().equals("")){
24                  if(size.trim().matches("^[1-9][0-9]*$")){
25                      width = size.trim();
26                  }else
27                  {
28                      width = "430";
29                  }
30              }else{
31                  width = "430";
32              }
33              paramJson.put("scene", param);
34              paramJson.put("path", "pages/ShouYe/ShouYe/ShouYe");
35              paramJson.put("width", width);
36              paramJson.put("is_hyaline", true);
37              paramJson.put("auto_color", true);
38              /**
39               * line_colorTake effect40               * paramJson.put("auto_color", false);
41               * JSONObject lineColor = new JSONObject();
42               * lineColor.put("r", 0);
43               * lineColor.put("g", 0);
44               * lineColor.put("b", 0);
45               * paramJson.put("line_color", lineColor);
46               * */
47  
48              printWriter.write(paramJson.toString());
49              // flushBuffering of Output Stream
50              printWriter.flush();
51              //Start getting data
52              BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
53              OutputStream os = new FileOutputStream(new File(codeUrl));
54              int len;
55              byte[] arr = new byte[1024];
56              while ((len = bis.read(arr)) != -1)
57              {
58                  os.write(arr, 0, len);
59                  os.flush();
60              }
61              os.close();
62          }
63          catch (Exception e)
64          {
65              e.printStackTrace();
66          }
67         return twoCodeUrl;
68     }

 

Leave a Reply

Your email address will not be published. Required fields are marked *