FTPClient cant get file listing from a directory name with space

Barun picture Barun · Jan 12, 2013 · Viewed 7.6k times · Source

I am using Apache FTPClient for getting files and sub-directory files listing. But it cant get listing of files from directory name with spaces. Here is an example - I tried it with two different directories:

    FTPClient client = new org.apache.commons.net.ftp.FTPClient();
    client.connect("ftp.domain.com");
    client.login("userid", "password");

    FTPFile[] names = client.listDirectories("ABC XYZ"); //Empty array
    FTPFile[] names2 = client.listDirectories("ABCXYZ"); //working

So directory name with spaces not returning anything. I tried to put "%20" and "+" at the place of space. Also I tried "\"ABC XYZ\"". But still is not working. Am I missing anything.

Answer

Tryder picture Tryder · Jul 19, 2015

This is an old one, but I recently ran into this problem and found a solution that seems to work for me. Use the escape character "\" to escape your spaces.

So for instance:

String path = "/Path/To/Folder With/Spaces";
path = path.replace(" ", "\\ ");
FTPFile[] listedDirectories = client.listDirectories(path);