输出实例:
输出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();
        }
    }
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐