java调用图片接口服务获取图片流,转base64前端展示

输出实例:
输出base64格式
接口代码:

    @RequestMapping(value = "/baseinfo")
    public RestResponse infoHe(@Param("bh") String bh, @Param("fjtype") String fjtype, HttpServletResponse response) {
        try {
            Map<String,Object> map = houseBiz.getPictureUrl(bh,fjtype);
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("ContentEncoding", "UTF-8");
            headers.put("X-Gisq-Token",map.get("token").toString());
            int timeOutSecend = 10000;
            // 请求地址参数
            Map<String, String> querys = new HashMap<>();
            //服务器上获取图片流
            if (!map.get("fjtype").toString().equals("{}")) {
                HttpResponse PreviewFile = HttpUtil.doPost(getPicturePreviewFile, "POST", headers, querys, (Map<String, String>) map.get("fjtype"), timeOutSecend);

                InputStream content = PreviewFile.getEntity().getContent();
                String base = inputStream2Base64(content);
                return RestResponse.ok(base);
            }
//            BufferedImage image = ImageIO.read(content);
//            if (image!= null){
//               ImageIO.write(image,"png",response.getOutputStream());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.ok("null");
    }

base64工具类:

    /**
     * 将inputstream转为Base64
     *
     * @param is
     * @return
     * @throws Exception
     */
    private static String inputStream2Base64(InputStream is) throws Exception {
        byte[] data = null;
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = is.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new Exception("输入流关闭异常");
                }
            }
        }

        return Base64.getEncoder().encodeToString(data);
    }

另一种方式,接受图片流 调用接口直接输出图片,前端处理图片

    @RequestMapping(value = "/baseinfo")
    public void infoHe(@Param("bh") String bh, @Param("fjtype") String fjtype, HttpServletResponse response) {
//        List<HouseInfo>  list = houseBiz.listALl(houseVo);
        try {
            Map<String,Object> map = houseBiz.getPictureUrl(bh,fjtype);
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("ContentEncoding", "UTF-8");
            headers.put("X-Gisq-Token",map.get("token").toString());
            int timeOutSecend = 10000;
            // 请求地址参数
            Map<String, String> querys = new HashMap<>();
            //服务器上获取图片流
            if (!map.get("fjtype").toString().equals("{}")) {
                HttpResponse PreviewFile = HttpUtil.doPost(getPicturePreviewFile, "POST", headers, querys, (Map<String, String>) map.get("fjtype"), timeOutSecend);

                InputStream content = PreviewFile.getEntity().getContent();
                BufferedImage image = ImageIO.read(content);
            if (image!= null){
               ImageIO.write(image,"png",response.getOutputStream());
            }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值