鍍金池/ 問答/Java  HTML/ 微信小程序在后臺如何將二進制流轉(zhuǎn)換成圖片

微信小程序在后臺如何將二進制流轉(zhuǎn)換成圖片

我在前端請求了小程序碼返回的是一堆亂碼,
java不太熟網(wǎng)上找了一個方法可以將二進制流和圖片互轉(zhuǎn),但是從微信小程序碼接口獲取的數(shù)據(jù)用這個方法無法獲取正確的圖片,不知道哪里有問題;
有沒有只在前端就能獲取小程序碼的方法

@RequestMapping("/qrTest")
public Map qrTest(Long hotelId, String appId, String token) {
    RestTemplate rest = new RestTemplate();
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + token;
        Map<String, Object> param = new HashMap<>();
        param.put("page", "pages/home/home");
        param.put("width", 430);
        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);
        System.out.println("調(diào)用生成微信URL接口傳參:" + param);
        // MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity requestEntity = new HttpEntity(param, headers);
        ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
        System.out.println("調(diào)用小程序生成微信永久小程序碼URL接口返回結(jié)果:" + entity.getBody());
        byte[] result = entity.getBody();
        System.out.println("\r\nold:" + Base64.encodeBase64String(result));
        inputStream = new ByteArrayInputStream(result);
        File file = new File("f:/abc.jpg");
        if (!file.exists()) {
            file.createNewFile();
        }
        outputStream = new FileOutputStream(file);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = inputStream.read(buf, 0, 1024)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.flush();
    } catch (Exception e) {
        System.out.println("調(diào)用小程序生成微信永久小程序碼URL接口異常");
    } 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;
}
回答
編輯回答
有點壞

大哥你path寫成page了

2017年5月28日 06:18