如何从mysql数据库中读取Blob(图像)数据并使用JSP在[浏览器]上显示[复制]

时间:2022-09-25 16:39:26

This question already has an answer here:

这个问题在这里已有答案:

I have a table in database. In that table one filed is of type BLOB contains image on that.I want to read the same image from database and want to display the image on jsp page by using tag.

我在数据库中有一个表。在那个表中,一个字段是BLOB类型包含图像。我想从数据库中读取相同的图像,并希望通过使用标记在jsp页面上显示图像。

// using code in jsp is

//在jsp中使用代码是

              <% 
                 Blob image;
                 image=blogd.getImage();
                 out.println(image);
                 %>
                <img src="<%=image.getBinaryStream() %>>" width="300px"  height="300px" />



 //  out.println(image);
  output of this particular line is
 org.hibernate.lob.SerializableBlob@c7014c
  • blogd is the object of Persitance java classs.
  • blogd是Persitance java类的对象。

1 个解决方案

#1


Try this

try {  
    response.setContentType("image/jpg");
    OutputStream out = response.getOutputStream();
    out.write(image.getBinaryStream());
    out.flush(); 
    out.close();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    // close the connexion;
}  

#1


Try this

try {  
    response.setContentType("image/jpg");
    OutputStream out = response.getOutputStream();
    out.write(image.getBinaryStream());
    out.flush(); 
    out.close();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    // close the connexion;
}