JPG image to JSP page
843836Mar 22 2004 — edited Mar 23 2004Hi 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