核心代码介绍
pox.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> </dependency>
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> </dependency>
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
QRcodeController.class
package com.example.demo.util;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;
@Controllerpublic class QRcodeController { @RequestMapping("/getCode") public void smallProgramCode(String param, String page, HttpServletResponse response) { OutputStream stream = null; try { String accessToken = getAccessToken(); response.setContentType("image/png"); String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken; Map<String, Object> paraMap = new HashMap<>(); paraMap.put("scene", param); paraMap.put("page", page); paraMap.put("width", 450); paraMap.put("auto_color", false);
paraMap.put("is_hyaline", false);
byte[] result = HttpClientUtils.doImgPost(url, paraMap);
response.setContentType("image/jpg"); stream = response.getOutputStream();
stream.write(result); stream.flush(); stream.close(); } catch (IOException e) { e.printStackTrace(); } }
public String getAccessToken() {
String appid = "wx5eeeqrwerwer5ee3f";
String appSecret = "c91207f9sdfsdfwetwe80f9051b0cd";
String accent_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
String url = accent_token_url.replace("APPID", appid).replace("APPSECRET", appSecret);
String result = HttpClientUtils.doGet(url); Map<String, Object> resultMap = (Map<String, Object>) JsonUtil.jsonToMap(result); System.out.println("access_token------>" + resultMap.get("access_token").toString()); return resultMap.get("access_token").toString(); }}
本内容属于网络转载,文中涉及图片等内容如有侵权,请联系编辑删除
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。