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

20 篇文章 0 订阅

依赖

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

记录一下,方便以后查阅

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成微信小程序二维码可以使用微信官方提供的API,具体可以参考微信官方文档。以下是使用Java实现生成微信小程序二维码的示例代: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class WeChatQRCode { public static void main(String[] args) throws IOException { // 小程序 appid String appid = "your appid"; // 小程序 appsecret String appsecret = "your appsecret"; // 获取 access_token 的接口地址 String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret; // 获取小程序的接口地址 String qrcodeUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token="; // 保存二维码的本地文件路径 String filePath = "qrcode.jpg"; // 发送获取 access_token 的请求 URL url = new URL(tokenUrl); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); byte[] buffer = new byte[1024]; int len; StringBuilder sb = new StringBuilder(); while ((len = in.read(buffer)) != -1) { sb.append(new String(buffer, 0, len)); } in.close(); String accessToken = sb.toString().split("\"")[3]; // 发送获取小程序的请求 url = new URL(qrcodeUrl + accessToken); conn = url.openConnection(); in = conn.getInputStream(); FileOutputStream out = new FileOutputStream(filePath); while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); in.close(); System.out.println("小程序已保存到:" + filePath); } } ``` 需要注意的是,生成小程序的接口有一些限制,例如必须是已发布的小程序二维码有效期为7天等,具体请参考微信官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值