一,获取小程序码的工具类(可直接套用)
//微信小程序生成二维码
public static Image getminiqrQr(String page,String sceneStr, String url) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
Map<String,Object> param = new HashMap<>();
param.put("scene", sceneStr);
param.put("page", page);
param.put("width", 300);
param.put("auto_color", false);
Map<String,Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity<String> requestEntity = new HttpEntity<String>(JSONObject.fromObject(param).toString(), headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
byte[] result = entity.getBody();
inputStream = new ByteArrayInputStream(result);
if(inputStream!=null){
Image img = ImageIO.read(inputStream);
if(img==null){
return null;
}
return img;
}
} catch (Exception e) {
System.out.println("调用小程序生成微信永久小程序码URL接口异常"+e);
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
二,在请求二维码时要将参数准备好,需要小程序的appID,secret 等参数,请求微信接口获取accseToken,然后再去请求另一个微信接口即可获取微信二维码的图片流
// 二维码
@RequestMapping("/ShareQrCode.do")
@ResponseBody //以响应的方式传数据所以没有返回值
public void ShareQrCode(HttpServletResponse response, HttpServletRequest request) {//H5ye页面在使用
// 小程序唯一标识 (在微信小程序管理后台获取)
String wxspAppid = "wx877d05714c6675ba";
// 小程序的 app secret (在微信小程序管理后台获取)
String wxspSecret = "e6d5bd8d5ed5795327328af95a587603";
// 授权(必填)
String grant_type = "client_credential";
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + wxspAppid
+ "&secret=" + wxspSecret;
String page = request.getParameter("page");
String scene = request.getParameter("scene");
try {
JSONObject accseToken = JSONObject.fromObject(MyUtilImpl.getURL(url));
String tokenUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
+ accseToken.get("access_token");
System.out.println(tokenUrl);
Image buffImg = MyUtilImpl.getminiqrQr(page, scene, tokenUrl);// 获取文件流
if (buffImg != null) {
ImageIO.write((RenderedImage) buffImg, "JPEG", response.getOutputStream());// 将文件流放入response中
} else {
String pathUrl = request.getSession().getServletContext().getRealPath("");
String srcImgPath = pathUrl + "resource/defaultImage/defaultQR.png";// 获取失败时默认图片显示
Image srcImg = ImageIO.read(new File(srcImgPath));// -----------------------------获取失败时的显示
ImageIO.write((RenderedImage) srcImg, "PNG", response.getOutputStream());// 将文件流放入response中
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
请求微信接口的工具类
//获取网络URL接口信息工具类
public static String getURL(String URL) throws Exception{
String result = "";
String urlNameString=URL;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
HttpURLConnection httpURLConnection = null;
if(connection instanceof HttpURLConnection){//能转化
httpURLConnection = (HttpURLConnection)connection;//强转
}
System.out.println(URL);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8"));//获取返回json 字符串流
String currentLineStr;
while((currentLineStr = bufferedReader.readLine()) != null){//读取行
result+=currentLineStr;
}
bufferedReader.close();
return result;
}