Saturday, November 7, 2009

Read Image into JSP




In the last post i explained about the reading of image from database to jsp file. This jsp will writes the image as it is in the uploaded size into the jsp. In that case you are fowarding byte array to the jsp and you are writing that byte array in the jsp.
You can also get the image from the database by specifying the action in the src attribute of the image tag as in the below tag.

<img height="600" width="500" src="readImage.do" id="image"/>

In this case the acion will get the byte array of the image from the database and you need to write that byte array into the response as show below....

byte imageArray[] = dao.readImage(imageid);
OutputStream output = response.getOutputStream();
output.write(imageArray);
output.close();

Using output stream you are writing the image array to response this will display the image in the jsp/browser.

In this case you can display the image size as you wish by specifying the height and width.

No comments: