• 获取小程序二维码


    public class GetQcCodeUtil {
        
        //获取二维码路径
        private static final String WxCode_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";
        //图片储存路径
        private static final String SAVE_PATH = "";
        
        /**
         * 获取二维码
         * @param accessToken
         * @param fileName 文件名称
         * @param param  二维码参数
         * @return
         */
        public static String getminiqrQr(String accessToken,String fileName,String param) {
            File file = new File(SAVE_PATH);
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String savePath = file.getPath() + File.separator + fileName;
            String qrCode = SAVE_PATH + File.separator + fileName;
            if (qrCode.contains("\")) {
                qrCode = qrCode.replace("\", "/");
            }
            try
            {
                String wxCodeURL = WxCode_URL.replace("ACCESS_TOKEN",accessToken);
                URL url = new URL(wxCodeURL);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");// 提交模式
                httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
                httpURLConnection.setReadTimeout(2000);//读取超时 单位毫秒
                // 发送POST请求必须设置如下两行
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
                // 发送请求参数
                JSONObject paramJson = new JSONObject();
                paramJson.put("scene", param);//参数
                paramJson.put("page", ""); //若小程序暂未发布 带page参数无作用
                paramJson.put("width", 400);//生成二维码的大小
                paramJson.put("is_hyaline", true);
                paramJson.put("auto_color", true);
                printWriter.write(paramJson.toString());
                // flush输出流的缓冲
                printWriter.flush();
                //开始获取数据
                BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
                OutputStream os = new FileOutputStream(new File(savePath));
                int len;
                byte[] arr = new byte[1024];
                while ((len = bis.read(arr)) != -1)
                {
                    os.write(arr, 0, len);
                    os.flush();
                }
                os.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return qrCode;
        }
    
    
        public static void main(String[] args) {
            String token = "";//若不知道怎么获取 请查看 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
            System.out.println(getminiqrQr(token,FormatTimeUtil.getTimeMillis()+"图片名称","参数"));
        }
    }
  • 相关阅读:
    LeetCode "Super Ugly Number" !
    LeetCode "Count of Smaller Number After Self"
    LeetCode "Binary Tree Vertical Order"
    LeetCode "Sparse Matrix Multiplication"
    LeetCode "Minimum Height Tree" !!
    HackerRank "The Indian Job"
    HackerRank "Poisonous Plants"
    HackerRank "Kundu and Tree" !!
    LeetCode "Best Time to Buy and Sell Stock with Cooldown" !
    HackerRank "AND xor OR"
  • 原文地址:https://www.cnblogs.com/ch94/p/14741262.html
Copyright © 2020-2023  润新知