Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Applet upload/download image to/from database via Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Applet upload/download image to/from database via Servlet" Watch "Applet upload/download image to/from database via Servlet" New topic
Author

Applet upload/download image to/from database via Servlet

Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
I have a applet that connect to a database via a servlet. On the applet, the user click "choose file", then the applet pop an open dialog, select the image it want to upload. Ultimately I want the image to upload to the database into a blob column as well as download back to the client machine. Does anyone know how to do it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41104
    
  45
You can use the Apache HttpClient library to upload the file from the applet, and the Apache FileUpload library to handle the upload in the servlet. The FAQ links to an article that describes both, I think in the "FileUpload" page. Note that the applet needs to be signed in order to be allowed access to the local file system.

What do you mean by "download back to the client machine" - the file already exists on the client, doesn't it?


Ping & DNS - my free Android networking tools app
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
What do you mean by "download back to the client machine" - the file already exists on the client, doesn't it?


"Download back to the client machine" meaning that I have the ability to select a blob that located on the database and download the blob back to my computer. How can I do that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41104
    
  45
You can simply put a link on a web page, and upon clicking the link, the file can be streamed to the client. Make sure you set the correct file name and content type.
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
Ulf Dittmer wrote:You can simply put a link on a web page, and upon clicking the link, the file can be streamed to the client. Make sure you set the correct file name and content type.


Do you think you can post some sample codes to help me a bit
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41104
    
  45
Code for what? Putting a link on a web page? Retrieving the file contents from the DB? Streaming the file contents to the client? What do you have so far?
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
Ulf Dittmer wrote:Code for what? Putting a link on a web page? Retrieving the file contents from the DB? Streaming the file contents to the client? What do you have so far?

+On the upload side:
I look at apache file upload, and it does not seem to have an example for custom upload to DB. If you have a sample code for accomplishing it, I would appreciate very much.
+On the download side: I have a servlet that can talk to a DB now. I can retrieve a blob black via "PrepareStatemet" and getBlob. But I am not sure how to stream that back to the client.

I just want to apologize if what I am asking is too much. I am new to servlet, and for this project, I choose the wrong design (applet-servlet-database), now nothing seems to work out right. So if you can help I would appreciate very much.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41104
    
  45
I look at apache file upload, and it does not seem to have an example for custom upload to DB.

The user guide shows how to obtain a File, InputStream or byte[] object for the uploaded file; with that, it should be easy to create a PreparedStatement that saves it to the DB, no?

I have a servlet that can talk to a DB now. I can retrieve a blob black via "PrepareStatemet" and getBlob. But I am not sure how to stream that back to the client.

The SimpleStream example in the CodeBarn shows how to do that: http://faq.javaranch.com/java/CodeBarnSimpleStream

I just want to apologize if what I am asking is too much.

It's not. This site is all about asking questions and learning about Java technologies, so ask away.
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
Thank you I would try my best
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet upload/download image to/from database via Servlet
 
Similar Threads
Image upload applet
image upload quary:need help
Uploading Image from jsp to database via servlet
Upload image in database
how to get this project