0

I'm currently trying to pull a file from an FTP server, but I've hit a snag in the process. The code below is a only partial working piece that:

  1. logs into the FTP server
  2. makes a few setting changes
  3. attempts to list the files found in that directory.

The issue: When the LIST command is pushed to the FTP server, it just hangs without timing out, erroring out, or tossing an exception. I actually accidentally left this process running over the weekend and it hung at this point the entire time. I've included the reply strings output from the FTP server, but I feel like there is more specific error information that I'm missing that would help me pinpoint this error.

Questions:

  1. Is there another method in the apache-commons-net FTPSClient list to output more specific error information from the FTP server? I looked through the JavaDocs for a method and didn't find anything specific - but I'm admittedly notorious for missing methods in JavaDocs (my vision is a bit bad...)
  2. Could I be missing a simple configuration step in the process of connecting to the FTP server? I've checked all of my configuration against the login information sent to me. Here is a screenshot of the Site Manager with server information from Filezilla (note: I've removed the ftp hostname, this is correct in the code). EDIT: The server is set to listen to port 991.

ftp_settings

  1. Could I be dealing with a port issue? I'm hoping if this is the case, then request 1 would be able to give me more detail...

We've tried this code in three different environments - one remote an two local. Any help on this would be much appreciated as both myself and a more senior developer on my team are stumped. Thank you for your time!

Main method:

    FTPSClient ftp = new FTPSClient();    

    boolean error = false;
    try {
      int reply;

      ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
      ftp.connect("SITE_NAME", 991);

      ftp.setFileType(FTP.BINARY_FILE_TYPE);
      System.out.println("Set file type to binary");

      ftp.sendCommand("PBSZ 0");

      ftp.sendCommand("PROT P");

      ftp.enterLocalPassiveMode();
      System.out.println("Set mode to passive (local).");

      //ftp.enterRemotePassiveMode();
      //System.out.println("Set mode to passive.");

      boolean success = ftp.login("USERNAME", "PASSWORD");

      if (!success) {
            System.out.println("Could not login to the server");
            return;
        } else {
            System.out.println("LOGGED IN SERVER");
        }

      ftp.setBufferSize(1024 * 1024);

      //ftp.sendCommand("OPTS UTF8 ON");

      String directory = ftp.printWorkingDirectory();
      System.out.println("Working directory: " + directory);

      int timeout = ftp.getConnectTimeout();
      System.out.println("Timeout Length: " + timeout);


        FTPFile[] list = ftp.listFiles();
        System.out.println("Generate List of Files");

        int length = list.length;
        System.out.println("Length: " + length);


      ftp.logout();
    } catch(IOException e) {
      error = true;
      e.printStackTrace();
    } finally {
      if(ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch(IOException ioe) {
          // do nothing
        }
      }
      //System.exit(error ? 1 : 0);
    }
}

Output from Protocol Command Listener:

220 Microsoft FTP Service
AUTH TLS
234 AUTH command ok. Expecting TLS Negotiation.
TYPE I
200 Type set to I.
Set file type to binary
PBSZ 0
200 PBSZ command successful.
PROT P
200 PROT command successful.
Set mode to passive (local).
USER *******
331 Password required
PASS *******
230 User logged in.
LOGGED IN SERVER
PWD
257 "/" is current directory.
Working directory: /
Timeout Length: 0
SYST
215 Windows_NT
PASV
227 Entering Passive Mode (204,179,168,88,195,91).
LIST
125 Data connection already open; Transfer starting.
Tabaker78
  • 49
  • 1
  • 1
  • 9
  • FTP works only on Ports 21 and 22 (SFTP)... You have it coded to the wrong port... – ryekayo Apr 10 '15 at 20:35
  • I had thought about that... but, if it was coded to the wrong port, wouldn't the connection to the server fail far before it gets to this point? – Tabaker78 Apr 10 '15 at 20:39
  • It depends on whether or not you have your server to listen on that port specifically... – ryekayo Apr 10 '15 at 20:41
  • I can tell you that logging into the server using the settings shown in my image above - the connection succeeds in both Filezilla and, at least it looks like via the output, here in this application as well. – Tabaker78 Apr 10 '15 at 20:45
  • Well after digging around, i am right and wrong. FTP usually works on Ports 21 and 22, however it can be set to work off different ports but by default will work on 21.. – ryekayo Apr 10 '15 at 20:45
  • Yeah, I tried coding to 21 and 22 and got timeouts for both. So, we can assume the server is set to listen to port 991. I'll edit my post. :) – Tabaker78 Apr 10 '15 at 20:48
  • As far as your other question. I would recommend enabling logging for your program. Logging can produce a stacktrace and give you an idea as to what is happening when you push the List button. – ryekayo Apr 10 '15 at 20:51

0 Answers0