Presenting image read from database in a html page
kiran mahavir
Ranch Hand
Joined: Jan 09, 2001
Posts: 35
posted
0
hello all, Please explain me the following concept - I have a image stored in a database - I want to rerieve it(in a servlet) and display it in a browser. We read the image using resultSet.getBinaryStream(). Now my problem is how to display the stream read.The browser only jpg or gif formats. so how to convert the stream into image and display in the browser. Thanking you in advance with regards Kiran
javed iqbal
Greenhorn
Joined: Jul 04, 2001
Posts: 6
posted
0
Try this code. I hope this will help you. I assume ur image is stored in OLEObject field. String filename="temp.jpg" or "temp.gif" String sql="Select image from mytable where imageId="120"; FileOutputStrean out; try{ File myf=new File(filename); out=new FileOutputStream(myf); PreparedStatement pst=cont.prepareStatement(sql); ResultSet rs=pst.executeQuery(); int readsize = 0; while (rs.next()) {
InputStream in = rs.getBinaryStream(1); byte bytes[] = new byte[8*1024]; int nRead = in.read(bytes); while (nRead != -1) { readsize = readsize + nRead; outFileStream.write(bytes,0,nRead); nRead = in.read(bytes); }//while }//while pst.close(); out.clsoe(); }//try catch(SQLException e) {} catch(Exception e) {} Now "filename" (jpg/gif) contanin bytestreams display this file into your browser.
This is what i learn from you.
javed iqbal
Greenhorn
Joined: Jul 04, 2001
Posts: 6
posted
0
Try this code.I hope this will work. String filename="temp.jpg" or "temp.gif"; FileOutPutStream out; try { File myf = new File(filename); out = new FileOutputStream(myf);
PreparedStatement stmt = cont.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); int readsize = 0; while (rs.next()) { InputStream in = rs.getBinaryStream(1); byte bytes[] = new byte[8*1024]; int nRead = in.read(bytes); while (nRead != -1) { readsize = readsize + nRead; out.write(bytes,0,nRead); nRead = in.read(bytes); }//while }//while stmt.close(); out.close(); in.close(); }//try catch(SQLException e) {} catch(Exception e) {} Now filename contains your jpg or gif byte streams. Display this file into your browser.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Presenting image read from database in a html page