Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File download link

 
harry flower
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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="ftp://ftp.site.com/dirname/file.zip">

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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The closest thing I have is a sample app that streams images from file.
http://simple.souther.us
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 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic