获取请求体中图片的二进制流直接解析成图片

//对path进行urlEncode
        path = UrlEncodeDecodeUtil.urlEnode(path);
        String url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={access_token}&path={path}";
        //新的业务逻辑
        final String imageJpeg = "image/jpeg";
        HttpHeaders headers = new HttpHeaders();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            List list = new ArrayList<>();
            list.add(MediaType.valueOf(imageJpeg));
            headers.setAccept(list);

            ResponseEntity<byte[]> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    new HttpEntity<byte[]>(headers),
                    byte[].class,authorization.getAuthorizerAccessToken(),path);

            byte[] result = response.getBody();

            BussinessLogger.printLogger(this,"二维码返回结果result:"+result);
//            inputStream = new ByteArrayInputStream(result);
            return result;
//            File file = new File(System.currentTimeMillis()+".jpeg");
//            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();

        }finally {
            if(inputStream != null){
                inputStream.close();
            }
            if(outputStream != null){
                outputStream.close();
            }
        }
//controller
@GetMapping(value = "qrCode",produces = MediaType.IMAGE_JPEG_VALUE)
public Object getQrcode(@RequestParam("appId") String appId,
                                          @RequestParam(value = "path",required = false) String path) throws IOException {}

  

原文地址:https://www.cnblogs.com/liangmm/p/12027845.html