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
Printable View
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
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:
So, when I try to run this code inside an applet, I get the following exception: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) {}
}
Any ideas?Quote:
sun.net.ftp.FtpProtocolException: PORT :501 PORT not allowed after EPSV ALL
Thanx,
Theodore
Maybe this could help,
http://groups.google.com/group/comp....934e2904a048e2
Thanx for the reply but I have already read this archive and that didnt help me.
Any other ideas?
Regards,
Theodore
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.
Look at the following java api. The method you are wanting to use is Ftp.downloadDir
http://www.jscape.com/sftp/
Thanx alot, but this is not a free javaftp client...Quote:
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:
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?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();
}
...
Thanx in advance,
THeodore
I believe since the FTPClient class you are using extends from SocketClient you can call connect() with this prototype:
Dont take my word for it I just gave it a quick look.Code:connect(InetAddress host, int port)
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