angular - 如何获取此图像并使用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());
}
解决方案
使用 HTTPCLIENT(如果 angular v4+)从 Angular 调用服务,如果 CORS 问题使用 proxy-config.json 文件,或者启用 cors。
从角度调用服务中检索响应
通过响应以角度显示图像
** 使用 CDN + 从后端到前端发送 URL 可能会更好——我的 2cents
推荐阅读
- node.js - 对于数量大于 10 的更新组,TransactWriteItems API 失败。错误:成员的长度必须小于或等于 10
- java - 使用存储库注释时出现 Spring NoSuchBeanDefinitionException
- java - 根据 mongo db 中的字符串列表过滤字符串
- python - Maya Python:使用 for 循环重命名对象
- android - 我的 Android 手机如何决定在浅色或深色主题之间切换闪屏?
- go - 如何将html表值获取到主程序
- codeigniter - 在codeigniter中搜索多个表数据
- amazon-web-services - AWS H2O 推理服务器 - 空结果
- grafana - 当用户单击下拉值时,是否可以在 grafana 中动态更改查询
- android - 使用 MediaCodec 在 Android 上处理 HDR10 视频