收藏
回答

java后台生成带参数小程序二维码图片错误,看不了是什么回事?(检查了APPID跟secret没错)

//生成二维码
@RequestMapping("createMySmallCode")
@ResponseBody
public static String createMySmallCode(Long shopId, HttpServletRequest request) {
    Result result = null;
    String path = null;
    String returnPath = null;
    String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WeiXinPayConfig.xcx_appid + "&secret=" + WeiXinPayConfig.xcx_secret;
    Map<String, String> resMap = new HashMap<>();
    try {
        String resultStr = HttpClientUtil.doGet(url, resMap);
        Map<String, String> map = JsonMapUtil.jsonToMap(resultStr);
        String accessToken = map.get("access_token");
        String urlC = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
        Map<String, Object> param = new HashMap();
        param.put("width", 420);
        param.put("scene", shopId);//设置的参数,小程序端可获取

        param.put("page", "/pages/pay/pay");

        MultiValueMap<String, String> headers = new LinkedMultiValueMap();
        RestTemplate rest = new RestTemplate();
        HttpEntity requestEntity = new HttpEntity(param, headers);
        ResponseEntity entity = rest.exchange(urlC, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
        byte[] data = (byte[]) entity.getBody();
        String filepath = "/home/sys/upload/shopCode/file/";

        File file = new File(filepath);
        if (!file.exists()) {
            file.mkdirs();
        }
        long currtime = System.currentTimeMillis();
        path = filepath + currtime + ".jpg";
        returnPath = currtime + ".jpg";
        FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
        imageOutput.write(data, 0, data.length);
        imageOutput.close();
    } catch (Exception var23) {
        var23.printStackTrace();
    }
    return request.getContextPath() + path;
}


最后一次编辑于  2020-08-20
回答关注问题邀请回答
收藏
登录 后发表内容
问题标签