JAVA之InputStream加载网络图片(填充到src)
Posted 黄小易
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA之InputStream加载网络图片(填充到src)相关的知识,希望对你有一定的参考价值。
@RequestMapping(value = "/queryCompanyImage", method = RequestMethod.GET)
@ResponseBody
public Result queryCompanyImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
Result result = new Result();
try {
Long fileId = Long.valueOf(request.getParameter("fileOssId"));
InputStream fileOssRecordInputStream = null;
fileOssRecordInputStream = ossHelpService.downloadPriFile(fileId);//这是一个阿里云OSS下载图片得到InputStream的方法
BufferedInputStream reader = new BufferedInputStream(fileOssRecordInputStream);
BufferedOutputStream writer = new BufferedOutputStream(response.getOutputStream());
byte[] bytes = new byte[1024 * 1024];
int length = reader.read(bytes);
while ((length > 0)) {
writer.write(bytes, 0, length);
length = reader.read(bytes);
}
reader.close();
writer.close();
result.setSuccess(true);
} catch (Exception ex) {
log.error("获取产品图片异常", ex);
return new Result(false, "获取产品图片异常");
}
return result;
}
<img src=‘${pageContext.request.contextPath }/rest/companyBorrow/queryCompanyImage?fileOssId=" + companyImage.fileOssId + "‘ style=‘width: 200px;height: 150px‘/>
以上是关于JAVA之InputStream加载网络图片(填充到src)的主要内容,如果未能解决你的问题,请参考以下文章
爬虫学习 08.Python网络爬虫之图片懒加载技术selenium和PhantomJS
08.Python网络爬虫之图片懒加载技术selenium和PhantomJS