Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTPClient cant get file listing from a directory name with space

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.

like image 519
Barun Avatar asked Jan 12 '13 08:01

Barun


1 Answers

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);
like image 75
Tryder Avatar answered Sep 24 '22 02:09

Tryder