依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.6</version>
</dependency>
下载微信小程序复合二维码
public void getWXQRCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
String deviceId = request.getParameter("device_id");
Assert.notNull(deviceId);
String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid="+ Constants.APP_ID+"&secret="+Constants.SECRET_KEY;
String result = HttpUtil.get(TOKEN_URL);
JSONObject resultJSON = JSONObject.parseObject(result);
String IMG_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+resultJSON.get("access_token");
//封装参数 文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getQRCode.html
JSONObject jsonObject = new JSONObject();
jsonObject.put("path", "pages/index/index?device_code=" + deviceId);
byte[] pic_bytes = HttpRequest.post(IMG_URL).body(jsonObject.toJSONString()).execute().bodyBytes();
ServletOutputStream outputStream = response.getOutputStream();
try {
outputStream.write(pic_bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStream.close();
}
}
记录一下,方便以后查阅