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:
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.
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.
Joined: Aug 29, 2007
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?
Joined: Mar 22, 2005
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.
As Ulf Mentioned, the commons net project is pretty easy to work with. You can also pull a file from an FTP server using java.net.URLConnection 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.