This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlets and images??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and images???" Watch "servlets and images???" New topic

servlets and images???

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi guys.
I've been searching these forums and haven't found any useful yet.
what I want to achieve is to store an image (jpeg or gif) in a MySQL database (3.23.49) using servlets.
1o.) First of all, I don't know how the field in the mysql database should be declared. i.e:
EXP_DATE not null,
image longblob,
does it have to be longblob? what are the possibles data types?
2o.) How do I retrive the image and put it in a bean property so I can access it with a 'getProperty' tag in a JSP page?
thanks in advance

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
You can try to convert the file to byte and store into database as byte data type.
I usually store image path into database and save image to server. I get the path and search the image when i want to retrieve the image.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
thanks steffy...
so, you store inthe database something like this:
in a text field in the database? did I get you right?
If I did, one thing comes to my mind. What happens if you don't have the image available in that folder, How do I handle that? If I have the image stored in a database, I can ask if the image field was null or something like that, and put a message like "photo not available"
Anyway, that was a good suggestion, never thought about it. How do I convert the file to abyte?
any other ideas??
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
I always stored only file name into database and the image path i declared in a properties file. For example in properties file, you can declare a variable RELATIVEPATH=/retrieveimg/img/
When i want to retrieve image i can combine RELATIVEPATH and image file name.
You can validate the image by 2 ways. The first you can validate the file name that retrieve from database.
For example,
String filename = rs.getString("imagename");
if (filename == null)
// Display message "Photo not available"
The second validate is check whether is the image file in the server.
String ABSOLUTEIMG="D:/IBM/VisualAge for Java/ide/project_resources/IBM WebSphere Test Environment/hosts/default_host/default_app/web/builder/wbuilder/img/"; thumbnailDir=new;
String[] list = thumbnailDir.list();
for (int i = 0; i<list.length-1;i++)
if (list[i].equals("home.gif"))

I never use servlet for my application but JSP. Hope this can answer your question.
Please refer to this site :
I agree. Here's the link:
subject: servlets and images???
jQuery in Action, 3rd edition