The moose likes Servlets and the fly likes File download link Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "File download link" Watch "File download link" New topic

File download link

harry flower

Joined: Aug 29, 2007
Posts: 19

I am a beginner in website development in java.
I wish to develop a website that allow users to download zip files from the web.
The zip files are stored in another FTP server (with login and password)
different from the web server.

I only know that one of the way is to add the link below in the webpage:

<A HREF="">

We can also insert login and password to this link. But there is
security issue with this solution. When user click this link,
a dialog box is displayed to prompt user whether he want to download
this file. However, on the browser status bar, it shows the the full
path of the zip file with its login and password.

Is there any other solutions to solve this problem?
It would also be good if anyone could provide some sample source codes
to solve this problem.

Thanks in advance.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I would put a servlet in between the user and the FTP server. If the user clicks a link that indicates that he wants to download a file (the link should contain the file name or ID), then the servlet fetches the file (using an FTP client library like Jakarta Commons Net), and then streams the file contents to the browser. That way, the user doesn't see where the file originates.
harry flower

Joined: Aug 29, 2007
Posts: 19
Hi Ulf,

Thanks for your reply.

Do you have a simple code to demonstrate your solution probably a page
with a link that download the file?

Where should I store the file when it is ftp locally?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
No, I don't have a ready-made sample. Commons FTP is pretty easy to get into, though, if you're familiar with FTP at all. The javadocs of the FTPClient class should get you going.

Fellow ranch moderator Ben Souther has a code example of how to stream binary data to a browser here. You should store the file someplace that's not publicly accessible, e.g. a subdirectory of WEB-INF, as Ben suggests.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

The closest thing I have is a sample app that streams images from file.
Look for SimpleStream.

As Ulf Mentioned, the commons net project is pretty easy to work with.
You can also pull a file from an FTP server using but the commons net libraries give you more control and are easier to debug.

You should be able to substitute the inputStream returned from getResourceAsStream with the inputStream returned by either of the two libraries mentioned above. I do this with one of the projects I work on and it works well.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: File download link
It's not a secret anymore!