当前位置:Gxlcms > 数据库问题 > java 将数据库中的blob字段转为图片显示在前端页面上

java 将数据库中的blob字段转为图片显示在前端页面上

时间:2021-07-01 10:21:17 帮助过:44人阅读

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

@RequestMapping(value = "/imageDisplay")
public void imageDisplay(String orgid, HttpServletResponse response, HttpServletRequest request) throws IOException, SQLException {
HashMap<String,Object> map = service.getById(orgid);//从数据库查询这条记录信息
if (map != null && map.size() > 0) {
byte[] bytes = (byte[]) map.get("orglogo");//orglogo为blob大字段 存储kb数据
response.setContentType("image/jpeg, image/jpg, image/png, image/gif"); //设置输出流内容格式为图片格式
InputStream in1 = new ByteArrayInputStream(bytes); //将字节流转换为输入流
IOUtils.copy(in1, response.getOutputStream());//将字节从 InputStream复制到OutputStream
}
String logoRealPathDir = request.getSession().getServletContext()
.getRealPath("/assets/images/icons.png");//获取默认图片路径
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
InputStream is = new FileInputStream(logoRealPathDir);

IOUtils.copy(is, response.getOutputStream());
}

java 将数据库中的blob字段转为图片显示在前端页面上

标签:tty   gif   dep   jpg   获取   图片显示   image   abc   流转   

人气教程排行