java后端请求微信小程序二维码生成接口,接收buffer数据转成图片

java后端请求微信小程序二维码生成接口,接收buffer数据转成图片

微信官方文档的二维码生成接口告诉我们返回的是图片的buffer数据。我起初尝试了很多次没有成功得到,要么就是使用spring生态的restTemplate工具类,只有指定为String的时候,我才能正常得到数据,但是也不知该如何使用,我又不想使用民间的请求工具包。

在网上找了很多资料,我也在此整理一下,以帮助后来者:

首先,show you the code.

	/**
     * 传递自定义参数获取二维码保存后并生成url给前端访问
     * @Author yeafel
     * @param scene
     * @param page
     * @param is_hyaline
     * @param auto_color
     * @return
     */
    @GetMapping("/getQrCodeImgUrl")
    public Object getQrCodeImgUrl(String scene,
                                  String page,
                                  Boolean is_hyaline,
                                  Boolean auto_color) {
        RestTemplate restTemplate = new RestTemplate();
        //首先获取ACCESS_TOKEN
        String getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
        JSONObject tokenResult = restTemplate.getForObject(getAccessTokenUrl, JSONObject.class);
        assert tokenResult != null;
        String accessToken = tokenResult.getString("access_token");

        //然后调用微信官方api生成二维码
        String createQrCodeUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
        //此处我是使用的阿里巴巴的fastJson
        JSONObject createQrParam = new JSONObject();
        createQrParam.put("scene", scene);
        createQrParam.put("page", page);
        createQrParam.put("is_hyaline", is_hyaline);
        createQrParam.put("auto_color", auto_color);

        PrintWriter out = null;
        InputStream in = null;
        try {
            URL realUrl = new URL(createQrCodeUrl);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数,利用connection的输出流,去写数据到connection中,我的参数数据流出我的电脑内存到connection中,让connection把参数帮我传到URL中去请求。
            out.print(createQrParam);
            // flush输出流的缓冲
            out.flush();
            //获取URL的connection中的输入流,这个输入流就是我请求的url返回的数据,返回的数据在这个输入流中,流入我内存,我将从此流中读取数据。
            in = conn.getInputStream();
            //定义个空字节数组
            byte[] data = null;
            // 读取图片字节数组
            try {
                //创建字节数组输出流作为中转仓库,等待被写入数据
                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                byte[] buff = new byte[100];
                int rc = 0;
                while ((rc = in.read(buff, 0, 100)) > 0) {
                    //向中转的输出流循环写出输入流中的数据
                    swapStream.write(buff, 0, rc);
                }
                //此时connection的输入流返回给我们的请求结果数据已经被完全地写入了我们定义的中转输出流swapStream中
                data = swapStream.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            String base64Code = new String(Objects.requireNonNull(Base64.encodeBase64(data)));
            //Base64转byte[]数组
            System.out.println(base64Code);
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }

        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return ResponseUtil.ok();
    }
}

下面来对上述代码进行描述:

  1. 首先我已经尽力把难以理解的地方进行注释
  2. 上例中请求微信官方生成二维码的API主要是用java的java.net包下的URL类进行的。我们可以看到我们使用URL类的实例打开connection之后,我们使用使connection的setDoOutput和setDoInput都变成了true。我们看下官方介绍
/**
     * Sets the value of the {@code doOutput} field for this
     * {@code URLConnection} to the specified value.
     * <p>
     * A URL connection can be used for input and/or output.  Set the DoOutput
     * flag to true if you intend to use the URL connection for output,
     * false if not.  The default is false.
     *
     * @param   dooutput   the new value.
     * @throws IllegalStateException if already connected
     * @see #getDoOutput()
     */
    public void setDoOutput(boolean dooutput) {
        if (connected)
            throw new IllegalStateException("Already connected");
        doOutput = dooutput;
    }
/**
     * Sets the value of the {@code doInput} field for this
     * {@code URLConnection} to the specified value.
     * <p>
     * A URL connection can be used for input and/or output.  Set the DoInput
     * flag to true if you intend to use the URL connection for input,
     * false if not.  The default is true.
     *
     * @param   doinput   the new value.
     * @throws IllegalStateException if already connected
     * @see     java.net.URLConnection#doInput
     * @see #getDoInput()
     */
    public void setDoInput(boolean doinput) {
        if (connected)
            throw new IllegalStateException("Already connected");
        doInput = doinput;
    }

从上文官方注释不难看出,如果把都connection的setDoOutput和setDoInput都设置为true,就可以使用URL connection来进行输入输出。什么意思呢?就是使用java.net.URL类的connection来进行请求的时候,还可以利用connection的输出流来向服务器传递参数使用connection的输入流得到服务器响应的结果数据,瞬间感觉java原生的URL还挺强大的。至于请求的过程我已经详细的把注释写进代码中了,请仔细浏览。

  1. 上述代码其实并不完整,我最终是要把得到图片的输出流存进华为云OBS对象存储中,但是,上述代码我已经把图片转成了base64编码字符串。亲测是转成图片后是正常图片,base64转图片地址链接。 base64编码都拿到了,就算你要存在本地也非难事。




总结:

一直以来网络请求在springBoot环境中喜欢使用restTemplate,在服务器返回文件流的时候非常棘手,这次使用了java.net.URL类进行请求,不但理解了,URL在openConnection之后,可以利用connection的输出流,写我的请求参数到请求的地址去,也可以使用connection的输入流得到请求之后的结果。很欣喜又成长了。继续加油!!!
                                   
                                   –yeafel

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值