1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java后端请求微信小程序二维码生成接口 接收buffer数据转成图片

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

时间:2023-12-26 06:18:20

相关推荐

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_TOKENString getAccessTokenUrl = "https://api./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./wxa/getwxacodeunlimit?access_token=" + accessToken;//此处我是使用的阿里巴巴的fastJsonJSONObject 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();}}

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

首先我已经尽力把难以理解的地方进行注释上例中请求微信官方生成二维码的API主要是用java的包下的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.URLConnection#doInput* @see #getDoInput()*/public void setDoInput(boolean doinput) {if (connected)throw new IllegalStateException("Already connected");doInput = doinput;}

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

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

总结:

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

–yeafel

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。