将URL图片转为输出流返回前端

查看图片->向内部影像系统发送请求->返回图片URL->把URL用流返回给前端:


    @RequestMapping(value = "image/view", method = {RequestMethod.POST, RequestMethod.GET})
    public Result viewImages(HttpServletRequest request, HttpServletResponse response) throws Exception {
        OutputStream os = response.getOutputStream();
        response.setContentType("image/jpg");
        byte[] data = LocalHttpClient.get(imageProperties.getNginxContext() + request.getParameter("path"));//get url
        os.write(data);
        os.flush();
        os.close();
        return ResultUtils.success();
    }



其中

public static byte[] get(String url) throws IOException {
        HttpUriRequest httpUriRequest = RequestBuilder.get().setUri(url).build();
        return httpClient.execute(httpUriRequest, new AbstractResponseHandler() {
            @Override
            public byte[] handleEntity(HttpEntity entity) throws IOException {
                return EntityUtils.toByteArray(entity);
            }
        }, HttpClientContext.create());
    }


你可能感兴趣的:(工作实践)