I want to download files which are in subfolder on FTP server but I see that my client can't access subfolders.
I wrote simple code to list all files in subfolder:
public static void main(String[] args) throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(HOST, PORT);
ftpClient.login(USER, PASS);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files = ftpClient.listFiles("/archive");
int length = files.length;
System.out.println("Number of files: " + length);
ftpClient.logout();
ftpClient.disconnect();
}
But length of files
array is zero.
When I run this program on main directory files are listed properly.
Your code is ok. It should work on most servers. Your code will send this command to the server:
LIST /archive
That's correct (assuming the server uses this path syntax [slash] to refer to a subfolder of the root folder).
But some (obscure) servers have problems with arguments passed to the LIST
command.
Try this code instead:
ftpClient.cwd("archive");
FTPFile[] files = ftpClient.listFiles();
The above will send:
CWD archive
LIST
Another approach worth trying is removing the leading slash, as the server may use a different path delimiter:
FTPFile[] files = ftpClient.listFiles("archive");