【发布时间】:2017-12-26 13:11:35
【问题描述】:
当我从数据库中获取查询成功实现的图像时,我无法在 thymleaf 中显示来自数据库的字节图像作为 blob 然后转换为字节 [],但它无法在 thymleaf 中显示,下面是代码
DaoImpl 代码 daoimpl 中用于从数据库中检索图像的函数
在这个功能中,使用查询从数据库中选择 blob,然后将 blob 转换为字节并将字节直接发送到控制器
@Override
public byte[] stockProductMenData()
{
Session session = getSession();
byte[] Imagebytes = null;
Query query = session.createQuery("select sp.stockProductPic "
+ "from StockType st,StockProduct sp"
+ " where st.stockTypeId=sp.stockTypeId and st.stockTypeName = :stockTypeName");
query.setParameter("stockTypeName","MEN");
List<Blob> list = query.list();
System.out.println("List size :"+list.size());
Iterator<Blob> itr = list.iterator();
while(itr.hasNext())
{
Blob blob = itr.next();
try {
Imagebytes =blob.getBytes(1,(int) blob.length());
System.out.println("dddddd"+Imagebytes);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("eeeeeee"+e);
}
}
控制器 在控制器中,我们接收字节数组并以 base64 编码并发送到 thymleaf 页面
@RequestMapping(value = "/mens" , method = RequestMethod.GET)
public ModelAndView custMen()
{
ModelAndView modelAndView =new ModelAndView();
byte[] picContent = customerDao.stockProductMenData();
byte[] encoded = Base64.getEncoder().encode(picContent);
System.out.println("ssssssssss"+picContent);
modelAndView.addObject("pic",encoded);
modelAndView.setViewName("mens");
return modelAndView;
}
带有百里香叶的前端 在这里我们收到图片的对象并使用 thymleaf 显示,但图片不显示
<a href="mens_single.html">
<div class="cbp-pgitem a3ls">
<div class="cbp-pgitem-flip">
<img alt="Image" th:field="${pic}" width="250" height="250"/>
</div>
</div>
【问题讨论】:
标签: java mysql spring hibernate thymeleaf