Retrieve a zip file from a BLOB field in a MySQL db
Hi,
Does anyone have any ideas on retrieving a zip file(or any file for that matter) from a BLOB field in a MySQL db?
The zip file contains logs from a test program, and got into the database via a short perl script I wrote. The script opens the file and reads it line by line, appending each line to a string whilst in binary mode. That string is then saved to the database the same way you would a normal string.
I need to retrieve the file and save it on the users local computer via my web app. Do I need to do the same as the perl script, but in reverse?
eg - read the BLOB from the data base and save it as a string
- create a new empty file on the users local computer called 'log.zip'
- open the file and write the string to it
Is there an equivalent to perls 'binmode' in JAVA, or something similar?
I've searched for info on this but the topics I find are concerned with displaying images from a BLOB on a page.
Any input appreciated. Cheers,
Olly
[1029 byte] By [
NievO] at [2007-11-26 8:43:46]

# 1
Hi Olly,
Your application code is executed on server side, and you have no possibility to write to user's local computer disk.
What you can do? You can save file from DB to server disk and display a link to it in your page. Clicking a link will open "Save file as..." dialog for user.
Saing file on server side is described here:
http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/fil e_upload.html
Thanks, Misha
(Creator team)
# 2
Hi Misha,
What do you mean?
> you have no possibility to write to user's local computer disk
The web app I am writing is to be run on a local network.
I already can open an INI file on the users local computer and edit it to save user preferences for my app.
What difference would it make if I opened a file, copied the binary string to it and saved it as file.zip?
Thanks for the link. I'll take a look
Olly
NievO at 2007-7-6 22:25:17 >

# 4
Hi Misha,
Thanks again for your reply, but I think we are talking about something different. Maybe I wasn't clear enough in explaining myself. I looked at the link you posted, and it concerns uploading a file from the client pc to a folder on the server pc.
I will try and explain my problem better. I have a mysql database running on the server with my web app. In one of the the tables in the database is a field which contains a zip file as a binary string or BLOB(Binary large object). I need to get this binary string out of the database and save it to the client pc as a zip file.
I know I can save files on the client PC as I save personal settings to an INI file on the client PC, similar to the way cookies work I suppose. This works regardless of what pc I use to access my web app from.
I hope this is a bit clearer.
NievO at 2007-7-6 22:25:17 >

# 5
You could employ the technique in this sample app:
Databases (Queries): Retrieving Binary Data - This project demonstrates how a web application can upload binary files, such as image files, into a database so users can retrieve them for viewing.
http://developers.sun.com/prodtech/javatools/jscreator/reference/code/sampleapp s/popups/Demo_BLOB.html
HTH,
Sakthi
# 6
Hi Olly,
I understand your question. But I wonder how you can save any info in file on client side, because it's imposiible, as far as I know. Could you provide code which does it? Do you perform it with Java or with JavaScript?
About saving file. If you have a BLOB field, you can get binary input stream this way:
ResultSet rs=sqlStatement.executeQuery(query) ;
InputStream st=rs.getBlob("MYBLOB").getBinaryStream();
Thanks, Misha
(Creatort team)
# 7
Hi Misha,
Thanks for all your input. Sorry if I have been a bit confusing, I've been getting client/server code muddled up. I did it a while back using Javascript on the client side, but as this is an internal tool I've decided just to save it on a mapped network drive for simplicity. It's actually a tar.gz file I have, and I've almost got it extracted now just a few creases to iron out.
Thanks again for your help
Olly
NievO at 2007-7-6 22:25:17 >
