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:
- logs into the FTP server
- makes a few setting changes
- 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:
- 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...)
- 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.
- 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.