小程序生成token 生成二維碼(2)

/**

*生成token

*/

public static String getAccessToken1() {
        String appId = PropertiesUtil.getProperties_1(
                "resources/project.properties", "video.appId");
        String appSecret = PropertiesUtil.getProperties_1(
                "resources/project.properties", "video.appSecret");
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                + appId + "&secret=" + appSecret;
        System.out.println("id:" + appId + "*********** secret:" + appSecret);
        String res = HttpUtil.doGet(url);
        JSONObject json = JSONObject.fromObject(res);
        String token = json.getString("access_token");

        return token;
    }

 

/**
     * 獲取小程序的token_access
     * 
     * @param token
     * @param create_time
     * @param secret
     * @param user_name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getWXACodeUnlimit_T")
    public ResultCode getWXACodeUnlimitT(
            @RequestParam(value = "scene", required = false) String scene,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "width", required = false) String width,
            @RequestParam(value = "is_hyaline", required = false) String is_hyaline,
            HttpServletRequest request) {

        Integer pwidth;
        if (StringUtils.isNotBlank(width)) {
            try {
                pwidth = Integer.parseInt(width);
            } catch (Exception e) {

            }
        }

        String md5 = MD5Util.string2MD5(scene);
        String fileName = MD5Utils.getMD5String(path + ":" + scene + ":"
                + width + ":" + is_hyaline)
                + ".png";
        String imageStorage = PropertiesUtil.getProperties_1(
                "resources/project.properties", "data.storage");

        String imageDomain = PropertiesUtil.getProperties_1(
                "resources/project.properties", "images.domain");
        String pic = imageStorage + "/images/qrcode/" + md5 + ".png";
        String pic1 = imageDomain + "/images/qrcode/" + md5 + ".png";

        File folder = new File(imageStorage);
        if (!folder.exists()) {
            folder.mkdirs();
        } else {
            if ((new File(fileName)).exists()) {
                String show_url = new String();
                show_url = imageDomain;
                show_url = show_url + "/qrcode/" + fileName;
                return ResultCodeDataStr.SUCCESS(pic1);
            }
        }

        String tem_access_token = tokenServiceImpl.getAccessTokenT();

        String imgUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
                + tem_access_token;
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(scene)) {
            map.put("scene", scene);
        }
        if (StringUtils.isNotEmpty(path)) {
            map.put("page", path);
        }
        map.put("width", width);
        map.put("is_hyaline", is_hyaline);

        HttpURLConnection conn = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            URL url = new URL(imgUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "application/json;charset=utf-8");

            JSONObject json = JSONObject.fromObject(map);
            String paramStr = json.toString();

            outputStream = conn.getOutputStream();
            OutputStreamWriter outer = new OutputStreamWriter(outputStream,
                    "UTF-8");
            outer.write(paramStr);
            outer.flush();

            inputStream = conn.getInputStream();

            bis = new BufferedInputStream(inputStream);
            byte[] arr = new byte[1024];
            int len;
            if ((len = bis.read(arr)) != -1) {
                if (new String(arr).indexOf("errcode") != -1) {
                    System.out.println("獲取小程序二維碼錯誤:" + new String(arr));
                    // wxx_access_token = "";
                    return ResultCodeDataStr.INFO("410", "圖片創建失敗", "false");
                }
            }
            os = new FileOutputStream(new File(pic));
            while (len != -1) {
                os.write(arr, 0, len);
                os.flush();
                len = bis.read(arr);
            }

            String show_url = new String();

            return ResultCodeDataStr.SUCCESS(pic1);

        } catch (Exception e) {
            System.out.println("獲取小程序二維碼錯誤:" + e);
            return ResultCodeDataStr.INFO("410", "圖片創建失敗", "false");
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章