mr. Vachovsky
mr. Vachovsky

Reputation: 1128

ftp client (from apache.org) and socket except

I use the library from apache.org and use the code from java2s.com:

import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.FileOutputStream;

public class Main {
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    client.connect("ftp.domain.com");
    client.login("admin", "secret");
    client.enterLocalPassiveMode();

    String filename = "sitemap.xml";
    fos = new FileOutputStream(filename);

    client.retrieveFile("/" + filename, fos);
    fos.close();
    client.disconnect();
  }
}

I downloaded the library, moved it to the lib folder and renamed it to cn.jar.

compiling: (under Windows 7)

javac -cp ".;lib\cn.jar" Main.java

running: (under Windows 7)

java -cp ".;lib\cn.jar" Main

and I've got: http://freelifer.narod.ru/some.png

How to fix it? What is wrong?

Upvotes: 0

Views: 2515

Answers (1)

Lachezar Balev
Lachezar Balev

Reputation: 12031

My guess: the FTP protocol defines two connections - a data connection and a control connection. Your sample code established the control connection successfully:

client.connect("ftp.domain.com");
client.login("admin", "secret");

The default constructor of FTPClient defines that the data connection should be established in active mode (meaning that the server will try to open a connection to your client when you request a file). I think that the data connection cannot be opened due to your firewall or some other network issue. The data connection is opened here:

client.retrieveFile("/" + filename, fos);

You may try passive mode or you may check your network settings again. Passive mode is entered by calling the enterLocalPassiveMode method of FTPClient. This method causes a PASV (or EPSV) command to be issued to the server. Example:

client.connect("ftp.domain.com");
client.login("admin", "secret");
client.enterLocalPassiveMode();

Cheers!

Upvotes: 1

Related Questions