Skip to content
Advertisement

Why can I retrieve file names but not file objects from the FTP server?

I’m trying to access files on an FTP server embedded in specialised hardware with the Apache Commons Net library. For some reason listNames() works, but listFiles() does not.

The documentation for the hardware is very limited, but it does at least say that active mode has to be used. Connection to the hardware is made over a local WiFi network with no Internet access. The code below runs from within an Android app.

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);

The output:

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

Judging by the error code 425, I suspect that there’s some issue related to active/passive mode, but can’t see how to clear it.

  • I have confirmed that the server is accessible and the files are browseable via the Turbo Client app on Android.
  • I have tried all the different possible FTPClientConfig options (e.g. FTPClientConfig.SYST_UNIX).

Advertisement

Answer

Martin Prikryl’s comments put me on the right track: calling setUnparseableEntries() caused listFiles() to start returning FTPFile objects that in turn returned usable information for getRawListing().

I’d already tried setUnparseableEntries() but I tried it again and this time, for some reason, it worked – so it must have needed some other setting to be made (possibly enterLocalActiveMode()) that wasn’t active at the time I initially tried it. Whatever the reason, listFiles() now returned a non-empty array.

I then extended ConfigurableFTPFileEntryParserImpl to create my own parser, so that I could use getName(), getSize() and isDirectory() on the FTPFile objects.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement