java下载微信小程序复合二维码

依赖

       <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();
        }
    }

记录一下,方便以后查阅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值