已经更新版本,下面是不保存本地直接将微信返@[TOC](微信生成小程序二维码 更新喽)
样例博客链接 : https://blog.csdn.net/weixin_37545129/article/details/88699623##
已经更新版本,下面是不保存本地直接将微信返回二维码流转换为二进制,上传云时直接将二进制上传,方法自行百度
直接贴代码
/**
* 获取小程序二维码
* @param imgPath 图片路径
* @param scene 传递的参数(自定义参数)
* @param config 配置(appId 和 secret 用于获取token)
* @return
*/
private String qrCode(String imgPath, String scene, ApiConfig config) throws IOException {
try {
//图片路径
String filePath0 = "/upload/image/" + DateTimeUtil.getDateTime8String();
//获取token (根据小程序 appId 和 secret 请求微信获取token )
String accessToken = AccessTokenApi.getAvailableAccessToken(config).getAccessToken();
//获取小程序二维码的接口
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", scene);
paramJson.put("page", "pages/index/index"); //已发布小程序页面,没有则写pages/index/index
paramJson.put("width", 430);
paramJson.put("auto_color", true);
printWriter.write(paramJson.toString());
System.out.println(imgPath + "=========图片路径=========================" + filePath0);
// flush输出流的缓冲
printWriter.flush();
//图片名称
String imgName = System.currentTimeMillis() +".png";
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
OutputStream os = new FileOutputStream(new File("D:\\", imgName));
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
return imgPath + filePath0 + imgName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取小程序二维码(返回二维码二进制) 将二进制上传至七牛云
*
* @param scene 传递的参数(自定义参数)
* @param appId --- secret 配置(appId 和 secret 用于获取token)
* @param path 小程序跳转路径
* @return
*/
private static byte[] qrCode(String scene, String appId, String secret, String path) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
BufferedInputStream inputStream = null;
byte[] bus = null;
try {
//获取token (根据小程序 appId 和 secret 请求微信获取token )
String token = TokenUtil.getToken(appId, secret);
//获取小程序二维码的接口
URL url = new URL(APP_URL + token);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", scene);
path = path == null ? "pages/index/index" : path;
paramJson.put("page", path); //已发布小程序页面,没有则写pages/index/index
paramJson.put("width", 430);
paramJson.put("auto_color", true);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
String fileKey = UUID.randomUUID().toString().replace("-" , "");
inputStream = new BufferedInputStream(httpURLConnection.getInputStream());//拿到二维码流
byte[] bytes = new byte[1024];
int b;
while ((b = inputStream.read(bytes)) != -1) {
outStream.write(bytes, 0, b);
}
bus = outStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}finally {
outStream.flush();
outStream.close();
inputStream.close();
}
return bus;//最后会返回二维码的二进制
}