我正在修改一些以前与我自己编写的 FTPS 库一起使用的代码。我被要求开始使用 Apache Commons Net 库(主要是 FTPClient 和 FTPSClient),但我在列出文件时遇到了问题。我读过其他问题,这不是 EnterLocalPassiveMode 问题( Apache Commons Net FTPClient and listFiles() ),因为我在连接之后、登录之前使用它。相同的代码在我设置的测试服务器上运行良好(也使用 Apache FTP) ,但在我需要它的服务器上不起作用。
我还尝试过使用“PBSZ 0”和“PROT P”命令,但它们并未在远程系统上实现。
502 PBSZ Command not implemented.
502 PROT Command not implemented.
代码:
FTPSClient ftpsclient = new FTPSClient(true); // Implicit SSL
ftpsclient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
ftpsclient.connect(HOST_ADDR, HOST_PORT); // Using port 990
ftpsclient.enterLocalPassiveMode();
ftpsclient.user(USERID);
ftpsclient.pass(PASSWORD);
ftpsclient.setFileType(FTP.BINARY_FILE_TYPE);
ftpsclient.changeWorkingDirectory(REMOTE_DIR);
ftpsclient.printWorkingDirectory();
FTPFile[] ftpfiles = ftpsclient.listFiles(); // This is where it breaks
我尝试显式指定目录并使用默认值:
FTPFile[] ftpfiles = ftpsclient.listFiles();
FTPFile[] ftpfiles = ftpsclient.listFiles(REMOTE_DIR);
...但两者给出相同的结果。这是调试信息的输出:
220 FTPS (Version Thu Dec 10 17:23:00 2015) server ready.
USER ****
331 Password required for ****.
PASS ****
230 User **** logged in.
TYPE I
200 Type set to I
CWD outbound/directory
250 CWD Command successful.
PWD
257 "/usr/path/to/outbound/directory" is current directory.
SYST
215 UNIX
PASV
227 Entering Passive Mode (XX,XX,XX,XX,24,140) ***Edit: port 6284
LIST
150 Opening data connection for '/bin/ls'.
然后在 30 秒后超时,堆栈跟踪如下:
Stack Trace: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.getReply(FTP.java:692)
at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1813)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3308)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3271)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)
我已检查我的防火墙设置,并且允许该主机通过端口 990 和 6200-6300 进行连接。
我还读过FTPClient.listFiles not working和 Java application hanging during LIST command to FTP Server (Apache Commons FTPClient)这些都没有帮助解决我的问题。
编辑:看起来 FTPClient 无法识别数据通道。我尝试上传文件而不是进行列表,但在出现“150 打开数据连接”消息后文件就终止了。我已确认通过 PASV 命令分配给数据连接的端口未被我们的防火墙阻止。
有什么想法吗?
最佳答案
我修好了。事实证明我正在使用无效路径列出目录。 Apache FTP 没有返回错误消息,而是关闭了连接。不知道为什么。不管怎样,它现在可以工作了。
关于java - Apache Commons Net FTPClient 将不会执行 listFiles(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215038/