Apache Commons FTPClient.listFiles
Solution 1
This seems like the same issue I had (and solved), see this answer:
Apache Commons Net FTPClient and listFiles()
Solution 2
After I set the mode as PASV
it is working fine now!
Thanks for all your efforts and suggestions!
Solution 3
I added client.enterLocalPassiveMode()
and it works:
client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();
Solution 4
Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.
Solution 5
First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.
Shyam Kumar Sundarakumar
I am a technologist working on Linux / Java. I love exploring and exploiting Open Source Software.
Updated on July 09, 2022Comments
-
Shyam Kumar Sundarakumar almost 2 years
I am using
org.apache.commons.net.ftp.FTPClient
in one of my applications to work with a FTP server. I am able toconnect
,login
,pwd
andcwd
. However, when I try tolist
the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the methodFTPFile[] listFiles()
, it returns an empty array ofFTPFile
.Please find below the code snippet where I am trying this:
String hostname = properties.getProperty("FTP_SERVER"); String user = properties.getProperty("FTP_USER"); String passwd = properties.getProperty("FTP_PASSWD"); FTPClient client = new FTPClient(); client.connect(hostname); client.login(user, passwd); String reply = client.getStatus(); System.out.println(reply); client.enterRemotePassiveMode(); client.changeWorkingDirectory("/uploads"); FTPFile[] files = client.listFiles(); System.out.println(files.length); for (FTPFile file : files) { System.out.println(file.getName()); } String[] fileNames = client.listNames(); if (fileNames != null) { for (String file : fileNames) { System.out.println(file); } } client.disconnect();
-
Noumenon over 7 yearsYes -- for example, the FTPFileListParser implentation that's passed to listFilemight assume lines must be a certain length, then miss all the files on a server with a different implementation of the LIST command.
-
Noumenon over 7 yearsSimilarly, the code I was looking at called a custom parser every time instead of allowing the auto-detect parser when the system type was normal Unix.