关于微信小程序开发java后台小程序码的生成

一,获取小程序码的工具类(可直接套用)

//微信小程序生成二维码
     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;  
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值