I have never saved and retrieved an image to and from the database before. I wrote down what I guessed would be the process. I would just like to know if this is correct though:
Save image:
- Select & Upload image file from jsp (Struts 2) which will save it as a .tmp file.
- Convert the .tmp file to a byte[] array (Java Server-Side)
- Store the byte[] array as a blob in the database (Java Server-Side)
Get image:
- Get the byte[] array from the database (Java Server-Side)
- Convert the byte[] array to an image file (Java Server-Side)
- Create the file in a location (Java Server-Side)
- Use an img tag to display the file (JSP Client-Side)
- Delete the file after it’s finished being used? (Java Server-Side)
I’m aware of the fact that it is highly recommended to not save & retrieve images to and from the database. I would like to know how to do it anyway.
Thanks
Almost correct.
It’s expensive and not so great to create the file on the fly and then delete it.
Yes, you store it as the raw bytes in the database, but the way to retrieve it and display it to a client machine is to implement a web handler that sets the content-type of the response to the appropriate MIME type and then dumps the bytes out to the response stream.