aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "File download link" Watch "File download link" New topic
Author

File download link

harry flower
Greenhorn

Joined: Aug 29, 2007
Posts: 19
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
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
harry flower
Greenhorn

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
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File download link
 
Similar Threads
File download link
Client side download from FTP Server (FTPClient from Apache)
how to copy files form server to local path?
problem retrieving a zip file from FTP server using org.apache.commons.net.ftp.
ANT FTP task: Software caused connection abort: connect