File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Presenting image read from database in a html page

 
kiran mahavir
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
javed iqbal
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic