Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPG image to JSP page

843836Mar 22 2004 — edited Mar 23 2004
Hi all,

Having a problem extracting the image from MySQL db to JSP. This problem has been posted before but can't find the answer.

I have a jsp with the following code:

<img name="Photo" src="/servlet/GetImage" width="250" height="250" alt="" style="background-color: #FFFFFF">

In my servlet GetImage, I have the following code where the blob is the second column in the resultset:

..............

if(results.next())
{
InputStream in = results.getBinaryStream(2);
response.setContentType("image/jpg");
int bytesRead = 0;
byte[] buffer = new byte[10]; //10k buffer
ServletOutputStream sout = response.getOutputStream();

while ( (bytesRead = in.read(buffer)) != -1) {
sout.write(buffer, 0, bytesRead); //write image
}
in.close();
sout.flush();
sout.close(); //close output stream
}
results = null;
}
catch(SQLException ex)
{
out.print(ex);
}

Hope someone has a suggestion.

cheers


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2004
Added on Mar 22 2004
5 comments
124 views