1

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.

// using code in jsp is

              <% 
                 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.
Naveen Kumar Mishra
  • 321
  • 1
  • 3
  • 16
  • 1
    First of all, don't use java code in JSP. Better to use JSTL & write business logic in server side. – OO7 May 07 '15 at 06:27
  • 1
    Get image data from DB, create output stream from response object, set the image type & write contents to output stream object. It will create image in JSP page. – OO7 May 07 '15 at 06:32
  • http://fdegrelle.over-blog.com/article-992927.html. Hope this helps – Ronald Randon May 07 '15 at 06:36

1 Answers1

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;
}  
OO7
  • 2,785
  • 1
  • 21
  • 33
  • i have to add in this tag. – Naveen Kumar Mishra May 07 '15 at 09:13
  • Then u have to create temp image file somewhere in the project directory or at suitable location & give the path of that directory including image file name in `` tag. – OO7 May 07 '15 at 09:15
  • @RequestMapping("/userblog.do") public String getblog(HttpServletRequest req)throws ServletException{ List blogdata=service.getAllBlog(); if(blogdata!=null){ Iterator it=blogdata.iterator(); while(it.hasNext()){ Object obj=it.next(); WriteBlogData bdata=(WriteBlogData) obj; Blob img=bdata.getImage(); // from here how to store in a folder } } } – Naveen Kumar Mishra May 07 '15 at 09:43
  • U hv `Blob` data. U can *get stream of bytes* calling `blob.getBinaryStream();`. Then create one `OutputStream` to write content into it. Follow [this](http://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/) tutorial. – OO7 May 07 '15 at 09:57