【发布时间】:2022-07-11 17:46:20
【问题描述】:
我正在尝试使用 Apache Commons Net 库访问嵌入在专用硬件中的 FTP 服务器上的文件。出于某种原因,listNames() 有效,但 listFiles() 无效。
硬件的文档非常有限,但它至少说必须使用主动模式。与硬件的连接是通过无法访问 Internet 的本地 WiFi 网络进行的。下面的代码在 Android 应用中运行。
ftpClient.connect(FTP_IP, 21);
Log.e(TAG, "reply string after connect():" + ftpClient.getReplyString());
ftpClient.enterLocalActiveMode();
ftpClient.enterRemoteActiveMode(InetAddress.getByName(FTP_IP), 21);
ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
Log.e(TAG, "reply string after login(): " + ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.LIST);
Log.e(TAG, "reply string for LIST: " + ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.NAME_LIST);
Log.e(TAG, "reply string for NLST: " + ftpClient.getReplyString());
remoteFiles = ftpClient.listFiles();
Log.e(TAG, "number of remote FTPFile objects returned: " + remoteFiles.length);
remoteFileNames = ftpClient.listNames();
Log.e(TAG, "remote file names returned: " + remoteFileNames);
输出:
2022-06-29 17:07:02.853 21263-22717/spinner.fakdown E/FTP: reply string after connect():220--- Welcome to *** FTP
220--- By *** ---
220 -- Version: 1.0 FTP-2015-04-08 - modify *** 2016-09-14 ver2 --
2022-06-29 17:07:02.875 21263-22717/spinner.f E/FTP: reply string after login(): 230 OK.
2022-06-29 17:07:02.881 21263-22717/spinner.f E/FTP: reply string for LIST: 425 No data connection
2022-06-29 17:07:02.888 21263-22717/spinner.f E/FTP: reply string for NLST: 425 No data connection
2022-06-29 17:07:03.235 21263-22717/spinner.f E/FTP: number of remote FTPFile objects returned: 0
2022-06-29 17:07:03.485 21263-22717/spinner.f E/FTP: remote file names returned: 6
根据错误码425判断,怀疑是主动/被动模式有问题,但不知道如何清除。
- 我已确认可以访问服务器,并且可以通过 Android 上的 Turbo Client 应用浏览文件。
- 我已经尝试了所有可能的
FTPClientConfig选项(例如FTPClientConfig.SYST_UNIX)。
【问题讨论】:
-
如果是主动/被动模式问题,
listNames也不起作用。 + 发布Apache Commons Net log file + 列表解析器可能不理解您的服务器列表格式。您需要为您的特定服务器类型配置解析器。甚至实现您自己的解析器。 => 试试FTPClientConfig.setUnparseableEntries。 -
@MartinPrikryl 非常有用,谢谢。我不知道那种显示 Apache Commons Net 日志的方法。 (顺便说一句,这提醒了我向 WinSCP 捐款,这在我的脑海中已经很久了——现在已经这样做了。)
标签: android ftp apache-commons-net