That has plenty of problems of its own, such as maintaining those links (especially when someone decides to change folder structures and forgets to tell the development team).
I was actually able to get the BLOB-from-DB2 approach to work, and it seems to be performing well for the type of traffic that the site will be used for. I think many of us are developing small to medium traffic sites where a few added milliseconds to load a photo isn't going to be very noticeable.
The trick was to use a servlet to handle the retrieval of the blob field from the database (which calls out to an EJB, which calls to a DAO) and map the html page's <img> tag to the servlet. Assuming a servlet that was mapped as "photo" in web.xml, notice how I point the image tag's src attribute to the servlet and pass in the id of the photo (in this case loading pictures of vehicles identified by VIN numbers):
<img id="photoImg" src="photo?serial=<%=vehicle.getVIN()%>" border="0" />
(The above code would go in a jsp page.)
Once the blob is retrieved by the servlet, then it gets converted to a byte array, which is then written directly to the servlet's outputstream:
Code:
public static byte[] getBytesFromBlob(Blob p_blob) throws Exception
{
int pos = 1;
int len = (int) p_blob.length();
byte[] bytes = p_blob.getBytes(pos, len);
return bytes;
}
__________________
Please login or register to view this content. Registration is FREE - free online training in Java, J2EE, and MySQL.
Please login or register to view this content. Registration is FREE - answers and advice from a geek who knows stuff.
|