首页 > 解决方案 > 如何获取此图像并使用http以角度4显示它?

问题描述

这是我的 java 春季启动代码。下面的代码是获取图像的其余函数。使用 http get 方法如何调用此方法并在角度 4 上显示图像?

 @RequestMapping(value = "/get-file",method = RequestMethod.GET)
    public void getImage(HttpServletResponse response) throws IOException
    {
        Long a = new Long(6);
        ImageModel imageModel= imageRepository.getOne(a) ;

        InputStream in = new ByteArrayInputStream(imageModel.getPic()); 
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
        //System.out.println("get image ");
    }

这是我的角度代码

public getCountry()
  {
    console.log("hey");
    let url = 'http://localhost:8080/get-file';
   return this._http.get(url,{ responseType: ResponseContentType.Blob }).map((res)=>res.blob());
  }

标签: angularspring-bootangular-httpangular4-forms

解决方案


  1. 使用 HTTPCLIENT(如果 angular v4+)从 Angular 调用服务,如果 CORS 问题使用 proxy-config.json 文件,或者启用 cors。

  2. 从角度调用服务中检索响应

  3. 通过响应以角度显示图像

** 使用 CDN + 从后端到前端发送 URL 可能会更好——我的 2cents


推荐阅读