4

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
Vivek
  • 957
  • 4
  • 12
  • 18

5 Answers5

0

enterRemotePassiveMode(); is used only for server to server connections, not client to server. Remove that line.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
jbak
  • 1
0

client.connect(host,990); Why are using the hardcoded port number? I suggest you change this line to use the port number passed to you function/method

For reasons that I did not have time to look at you code hangs and this line

  client.completePendingCommand();

before the server disconnects it. I am not sure if you are not getting the exception or you are just not waiting long enough for you code to throw the exception

sheu
  • 284
  • 5
  • 13
0

You need to check status after each client(socket) method. Especially after client.connect() and client.login().

Example:

client.connect(host, 990);
System.out.print(client.getReplyString());
...
client.login(username,password);
System.out.print(client.getReplyString());

If the method failed (error), the getReplyString() is usually very descriptive about what you did wrong.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

The Port Number you use for FTP - 21 and for SFTP is 22

I has some what same requirement , to download file from different servers , so some times i had to use 21 and some time 22 .

I tried below approach

FTPClient ftpClient = new FTPClient();

        int port = 21;
        try {

            try{
                ftpClient.connect(serverName, port);
            }catch(Exception e){
                ftpClient.connect(serverName, 22);  
            }

                try {
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.changeWorkingDirectory(workingDir);


            String fileName = "fileToDownLoad";

            File downloadFile = new File(fileName);

            OutputStream localOutputStream = new BufferedOutputStream(
                    new FileOutputStream(downloadFile));
            InputStream fptInputStream = ftpClient.retrieveFileStream(folder
                    + "/" + file);
            byte[] bytesArray = new byte[4096];
            int bytesRead = -1;
            while ((bytesRead = fptInputStream.read(bytesArray)) != -1) {
                localOutputStream.write(bytesArray, 0, bytesRead);
            }

            boolean success = ftpClient.completePendingCommand();
            if (success) {

            }
            localOutputStream.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
Ashish Shetkar
  • 1,414
  • 2
  • 18
  • 35
0

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".

Vladimir Dyuzhev
  • 18,130
  • 10
  • 48
  • 62
  • thanks a lot for the reply...I am a newbee with this. I did changed my code to move passive mode after login and removed remote passive mode, but no change in the result. My file is still not trasferred and I don't see anything while retreiving the remote file – Vivek Apr 28 '11 at 21:59
  • @Vivek how did you solve it ? – Hard Worker Dec 12 '16 at 07:26