Retreiving an image using JDBC

maher dabbas

Greenhorn

Posts: 26

posted 14 years ago

Hi all I'm trying to retreive an image from Oracle database,stored as BLOB, and to display this image on an Html page genarated by JSP. first i've wrote a Bean to retreive the image , the code is as follows:

Blob a1=res.getBlob("item_pic"); long len = a1.length(); int len2=(int)len; byte[] data= a1.getBytes(1,len2); return data. then in the jsp, i've wrote a code to print the byte array as follows:

for (int i = 0; i <data.length;i++) { out.print(data[i]); } But the result is a large amount of numbers printed on the screen ,not the image!!! Can any body help me on this please? Regards Maher Dabbas.

Bonnie Beach-Morin

Greenhorn

Posts: 13

posted 14 years ago

The out variable is a javax.servlet.jsp.JspWriter, which extends java.io.Writer. Writer classes print/write character representations of data. So, out.print(data[i]) is printing a character representation of each byte in the BLOB. In order to display an image in a page, the browser is going to look for an <img> tag with the src attribute pointing to a file on a server or URL, or as a download link. If you really, really want to display the image in a page along with other text, the only way I can think of is to extract the BLOB and write it to an intermediate file, then reference that intermediate file in the src attribute. If you want get the InputStream from the BLOB by calling blob.getBinaryStream() (make sure your jdbc drivers support BLOBs well), and feeding it to the ServletOutputStream of the response variable, it should allow a user to either download the image or view it in a browser window.

Hello guys. Why is this so difficult to achieve..?? . This is my situation: 1) I have stored an image in a blob field in mysql (ITEM_IMAGE). 2) Now, my index page is generated dynamically. So what I do is select all the items that are 'special', create a bean for every item and put them in a vector, something like this:

and this is makeBean:

SO it is a bit different of what Rene does. Actually, I haven't figured out how to display the image in my jsp page, coz in the way I'm doing it the servlet would need to get the blob from the DB,transform it some way ( ) and put it in the bean together with the other fields, so it can be used in the JSP page. do you guys have any suggestion as how I can achieve that? I'd appreciate any help. thanks

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury