-
January 28th, 2008, 02:21 PM
#1
Download directory using FTP
Hi everyone. I am looking for an easy way to download the contents of a whole directory from an FTP server using Java. Any ideas?
Regards,
Theodore
-
January 28th, 2008, 06:49 PM
#2
Re: Download directory using FTP
Ok, I have another question. I am trying to simply download a file (known) URL from an FTP server I have. Here's the code:
Code:
try{
URL url = new URL("ftp://USERNAME:PASSWORD@IP:21/DIR/FILE");
try{
URLConnection con = url.openConnection();
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
FileOutputStream out = new FileOutputStream("C:\\File");
int i = 0;
byte[] bytesIn = new byte[1024];
while ( (i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
System.out.println(i);
}
out.close();
in.close();
}
catch (IOException mal) {System.out.println(mal);}
}
catch (MalformedURLException mal) {}
}
So, when I try to run this code inside an applet, I get the following exception:
sun.net.ftp.FtpProtocolException: PORT :501 PORT not allowed after EPSV ALL
Any ideas?
Thanx,
Theodore
-
January 29th, 2008, 12:50 AM
#3
Re: Download directory using FTP
Good Luck
HackmanC
-
January 29th, 2008, 08:59 AM
#4
Re: Download directory using FTP
Thanx for the reply but I have already read this archive and that didnt help me.
Any other ideas?
Regards,
Theodore
-
January 29th, 2008, 01:00 PM
#5
Re: Download directory using FTP
The whole point was ... use apache commons.
Like you saw, in the googling, it's a feature which gives a lot of trouble, with SUN libraries.
Good Luck
HackmanC
-
January 31st, 2008, 05:55 PM
#6
Re: Download directory using FTP
Look at the following java api. The method you are wanting to use is Ftp.downloadDir
http://www.jscape.com/sftp/
-
February 7th, 2008, 07:45 AM
#7
Re: Download directory using FTP
Originally Posted by vglass@jscape.com
Thanx alot, but this is not a free javaftp client...
-
February 9th, 2008, 02:14 PM
#8
Re: Download directory using FTP
Ok guys, as HackmanC suggested I used apache common jar files and I managed to open ftp connections. Now I have another question. The sample code I found downloads a file and its sth like this:
Code:
ftp.connect(server);
...
reply = ftp.getReplyCode();
...
if (!ftp.login(username, password)) {
ftp.logout();
}
else
{
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
OutputStream output;
output = new FileOutputStream(fileNameTarget);
if (ftp.retrieveFile(pathToDownload, output))
System.out.println("FILE DOWNLOADED!");
else
System.out.println("A PROBLEM OCCURED WHILE TRYING TO DOWNLOAD THE FILE!");
output.close();
ftp.logout();
}
...
OK this works fine, but I was wondering if I can change the port on which the ftp client is connected. I suppose the above code uses by default, port 21. Can I change the port?
Thanx in advance,
THeodore
-
February 9th, 2008, 03:23 PM
#9
Re: Download directory using FTP
I believe since the FTPClient class you are using extends from SocketClient you can call connect() with this prototype:
Code:
connect(InetAddress host, int port)
Dont take my word for it I just gave it a quick look.
-
February 18th, 2008, 07:18 PM
#10
Re: Download directory using FTP
Thanx alot. I used the connect() method with the port argument (and even tried setDefaultPort(). The problem is that I want to use port 22, in order to connect to a sftp server. And I get a java exception which is like:
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: Protocol mismatch.
.....
Can I not achieve sftp connection using the apache class?
Thanx in advance,
Theodore
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|