码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA之InputStream加载网络图片(填充到src)

时间:2016-12-23 13:30:58      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:err   load   str   填充   eof   cep   []   public   throw   

@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)

标签:err   load   str   填充   eof   cep   []   public   throw   

原文地址:http://www.cnblogs.com/hjy1017/p/6214299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!