小程序码 踩坑记录(buffer转成图片显示问题)

废话不多说,我相信大多数人卡在了,从微信接口取到了图片的 buffer(一堆乱码),然后就不知道怎么办了,我也是用了一天时间才搞清楚怎么回事儿。。。自己笨了点,没办法。
直接上代码:

 

try {
            String appId = ResourceUtil.getConfigByName("wx.mini.appId");
            String secret= ResourceUtil.getConfigByName("wx.mini.secret");
            String getAccessTokenURL = ResourceUtil.getConfigByName("wx.mini.getQrCode");
            String reqAccessTokenURL = String.format(getAccessTokenURL,appId,secret);
            String respResult =  this.get(reqAccessTokenURL);
            JSONObject respResultJsonObject = (JSONObject) JSON.parse(respResult);
            logger.info(respResultJsonObject);
            Object tokenObject =  respResultJsonObject.get("access_token");
            String token = tokenObject.toString();
            Map<String, Object> resultObj = new HashMap();
            PrintWriter out = null;
            //BufferedReader in = null;
            InputStream in = null;
            String result = "";
            try {
                URL realUrl = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token);
                // 打开和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());
                // 发送请求参数
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("scene", ""); //参数自定义
                jsonObject.put("page","");//要生成小程序码的链接
                jsonObject.put("width",300);
                out.print(jsonObject);
                // flush输出流的缓冲
                out.flush();
                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);
                    }
                    data = swapStream.toByteArray();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return new String(Base64.encodeBase64(data));
            } 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();
                }
            }
//            resultObj.put("result",result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }

上面的代码只需要放到一个类里面,直接就可以运行。红色部门的代码是把微信接口返回的buffer转换成了base64字符串

然后用在线转换工具就可以直接看到图片了  https://tool.jisuapi.com/base642pic.html

 或者将base64字符串返回给小程序前端,

<image src="https://img-blog.csdnimg.cn/2022010613270196202.png"></image>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值