I have a servlet that retrieves an image from a mysql database, then the image is displayed within the
JSP that calls the servlet. The images themselves are uploaded into the database via another servlet. They are stored as type Blob and retrieved as the same type. Then they are read into a Byte[] array.
A snippet of my servlet code that retrieves the image from the db:
- Code: Select all
//…
bufout = new BufferedOutputStream(res.getOutputStream());
int int_blob_length = (int)blob_length;
byte by[] = new byte[int_blob_length];
int index = 0;
while ( index != -1 ) {
index = in.read( by, 0, int_blob_length);
//bufout.write( by, 0, int_blob_length);
//this if clause is a newer update to my code, because I was getting
//ArrayIndexOutOfBoundsException’s, and this clause fixed that issue
if(index = = -1) {
bufout.write( by, 0, 0);
} else {
bufout.write(by,0,index);
}//if
}//while
bufout.flush();
bufout.close();
//…
Within the JSP page there are 5 calls to the servlet via statements below :
- Code: Select all
<img src = "anything.viewimage?acc=4750&num=image1">
<img src = "anything.viewimage?acc=4750&num=image2">
<img src = "anything.viewimage?acc=4750&num=image3">
<img src = "anything.viewimage?acc=4750&num=image4">
<img src = "anything.viewimage?acc=4750&num=image5">
The problem I’m experiencing is that more often than not, one of the 5 images are not being displayed . Instead of the image I’m getting a
red X. My
JSP page is setup with javascript code as well, which involves these 5 images. There is one large image, and 4 thumbnails beneath it. The user clicks on a thumbnail to enlarge it, thus changing the main image above to the full size of the thumbnail.
I think what’s happening is that the
index variable is –1 at some point and the image isn’t coming through the buffer…but why would it go to –1? Can someone please help me decipher the problem? I want to have a smooth running servlet for retrieving images from the database…your input is appreciated.
Thank You

Love2Java