zoukankan      html  css  js  c++  java
  • Java获取微信小程序二维码

    1. tip:通过该接口,仅能生成已发布的小程序的二维码。
    2. tip:可以在开发者工具预览时生成开发版的带参二维码。
    3. tip:接口A加上接口C,总共生成的码数量限制为100,000,请谨慎调用。
    4. tip: POST 参数需要转成 json 字符串,不支持 form 表单提交
    5. tip: auto_color line_color 参数仅对小程序码生效。
        /*
         * 获取二维码
       * 这里的 post 方法 为 json post【重点】
    */ @RequestMapping("/getCode") public ResponseMsg getCodeM(HttpServletRequest request) throws Exception { String imei ="867186032552993"; String page="page/msg_waist/msg_waist"; String token = getToken(); // 得到token Map<String, Object> params = new HashMap<>(); params.put("scene", imei); //参数 params.put("page", "page/msg_waist/msg_waist"); //位置 params.put("width", 430); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token); // 接口 httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); String body = JSON.toJSONString(params); //必须是json模式的 post StringEntity entity; entity = new StringEntity(body); entity.setContentType("image/png"); httpPost.setEntity(entity); HttpResponse response; response = httpClient.execute(httpPost); InputStream inputStream = response.getEntity().getContent(); String name = imei+".png"; saveToImgByInputStream(inputStream,"D:\",name); //保存图片 return null; } /* * 获取 token
       * 普通的 get 可获 token
    */ public String getToken() { try { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("grant_type", grant_type); map.put("appid", appid); map.put("secret", secret); String rt = HttpUtils.sendGet(TOKEN_URL, map); System.out.println(rt); JSONObject json = JSONObject.parseObject(rt); if (json.getString("access_token") != null || json.getString("access_token") != "") { return json.getString("access_token"); } else { return null; } } catch (Exception e) { log.error("# 获取 token 出错... e:" + e); e.printStackTrace(); return null; } } /** * 将二进制转换成文件保存 * @param instreams 二进制流 * @param imgPath 图片的保存路径 * @param imgName 图片的名称 * @return * 1:保存正常 * 0:保存失败 */ public static int saveToImgByInputStream(InputStream instreams,String imgPath,String imgName){ int stateInt = 1; if(instreams != null){ try { File file=new File(imgPath,imgName);//可以是任何图片格式.jpg,.png等 FileOutputStream fos=new FileOutputStream(file); byte[] b = new byte[1024]; int nRead = 0; while ((nRead = instreams.read(b)) != -1) { fos.write(b, 0, nRead); } fos.flush(); fos.close(); } catch (Exception e) { stateInt = 0; e.printStackTrace(); } finally { } } return stateInt; }
  • 相关阅读:
    Vue收集表单数据
    vcloak、vonce、vpre
    自定义指令总结
    vhtml指令
    Vue模板语法
    vtext指令与插值语法的区别
    Vue过滤器
    sharepoint获取文件的ICON
    Sharepoint中添加/编辑/删除Webpart的几种方法
    [转]客户端input file控件,C#多文件上传
  • 原文地址:https://www.cnblogs.com/lemon-flm/p/8571915.html
Copyright © 2011-2022 走看看