aspose file tools
The moose likes JDBC and the fly likes Presenting image read from database in a html page Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Presenting image read from database in a html page" Watch "Presenting image read from database in a html page" New topic
Author

Presenting image read from database in a html page

kiran mahavir
Ranch Hand

Joined: Jan 09, 2001
Posts: 35
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
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
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
 
Similar Threads
Retrieve Images from oracle
Displaying a file on the browser without the download box.
2nd Trial - Algorithm (load,store and display) an image from db source to JSF Page sink (at runtime)
Display pictures in an applet
To retrieve an image from the database and display it in a JSP page