生成小程序二维码

作者: Leslie_Lee | 来源:发表于2020-12-25 15:53 被阅读0次



    public String erCodeUrl1(String toPath, String param, String erCodeName) throws Exception { String imgPath = "PROGRAM/IMG/erCode/" + erCodeName + ".png"; File targetFile = new File(imgPath); if (!targetFile.exists()) { targetFile.getParentFile().mkdirs(); logger.info("==============文件不存在=============="); logger.info("========生成微信小程序码开始==========="); String accessToken = accessTokenUtil.getAccess_token(); URL getCodeUrl = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken); HttpURLConnection httpURLConnection = (HttpURLConnection) getCodeUrl.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); JSONObject paramJson = new JSONObject(); PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); paramJson.put("page", toPath); paramJson.put("scene", param); paramJson.put("auto_color", false); printWriter.write(paramJson.toString()); printWriter.flush(); String contentType = httpURLConnection.getContentType(); logger.info("contentType " + contentType); logger.info("responseMessage:" + httpURLConnection.getResponseMessage()); if (contentType.contains("json")) { throw new Exception("调用微信小程序生成接口出错,token失效"); } else { BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); //小程序码图片写出存储的路径 OutputStream ost = new FileOutputStream(targetFile); int len; byte[] arr = new byte[1024]; while ((len = bis.read(arr)) != -1) { ost.write(arr, 0, len); ost.flush(); } ost.close(); logger.info("========生成微信小程序码结束==========="); return fileUrl + "/drug_store/" + imgPath.replace("PROGRAM", ""); } } else { logger.info("文件已存在==直接返回地址======"); return fileUrl + "/drug_store/" + imgPath.replace("PROGRAM", ""); }}

    相关文章

      网友评论

        本文标题:生成小程序二维码

        本文链接:https://www.haomeiwen.com/subject/uglrgktx.html