【问题标题】:Why can I retrieve file names but not file objects from the FTP server?为什么我可以从 FTP 服务器检索文件名但不能检索文件对象?
【发布时间】: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


【解决方案1】:

Martin Prikryl 的 cmets 让我走上了正轨:调用 setUnparseableEntries() 导致 listFiles() 开始返回 FTPFile 对象,而这些对象又返回了 getRawListing() 的可用信息。

我已经尝试过setUnparseableEntries(),但我又试了一次,这一次,出于某种原因,它起作用了——所以它一定需要进行一些其他设置(可能是enterLocalActiveMode()),而这些设置在我最初尝试的时间。不管是什么原因,listFiles() 现在返回了一个非空数组。

然后我扩展了ConfigurableFTPFileEntryParserImpl 以创建我自己的解析器,这样我就可以在FTPFile 对象上使用getName()getSize()isDirectory()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多