connect to ftps site via java

Vivek picture Vivek · Apr 28, 2011 · Viewed 10.8k times · Source

I am trying to connect to a clients ftps server via my java code. I am using apache commons library to do so. However, I am not able to do so. Can anyone please help me with this.

The client server uses FTPS/Implicit SSL connection and uses Passive mode for data Connections.

My code is as follows:

public static void connectServer(String host, int port, String userName, String password) throws Exception {
    FTPSClient client = new FTPSClient("SSL", true);
    String remote = "Contact.xml";

    File inFile = new File("C:/Documents/Applications/Contact.xml");
    File outFile = new File("C:/Documents/Applications/Sample.xml");

    InputStream input = new FileInputStream(inFile);
    InputStream out = new FileInputStream(outFile);

    try {

        if(client.isConnected()){
            client.disconnect();
        }

        client.connect(host,990);
        client.enterLocalPassiveMode();
        client.enterRemotePassiveMode();

        client.login(userName, password);

        client.setBufferSize((int)inFile.length()+100);
        client.completePendingCommand();

        System.out.println(client.printWorkingDirectory());
        System.out.println(inFile.getAbsolutePath());

        client.storeFile(remote, input);
        out = client.retrieveFileStream("/folder/inputfeed.xml");

        client.completePendingCommand();
        client.logout();

    } catch (Exception e) {
    e.printStackTrace();
        System.err.println(client.getReplyString());

    } finally {
        out.close();
        input.close();
        client.disconnect();
    }
}

This code does not throw any exception, but I don't see the file being copied to server, neither any data being copied to my InputStream. Also, sysout statement for getting the working directory returns me the correct directory. I am also able to connect to server via Filezilla and WinSCP.

Please help, I am getting stuck with this.

Thanks

Answer

Vladimir Dyuzhev picture Vladimir Dyuzhev · Apr 28, 2011

Why are you entering passive mode before login()?

I suspect that could be the issue, because the symptoms are those of active mode, where the connection cannot be established due to FW rule DROP (not REJECT; when rejected the exception is thrown right away, but DROP can hang forever).

P.S. Also, not clear what is "remote" passive mode; the only one that makes difference is "local".