FTPClient cant get file listing from a directory name with space
Asked Answered
A

3

6

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.

Austen answered 12/1, 2013 at 8:12 Comment(5)
Are you sure directory ABC XYZ exists where you're looking for it?Haddock
@JimGarrison Yes it exists. And I can open it with other ftp manager and even with browsers.Austen
Does directory ABC XYZ contain any subdirectories? That's what you're asking for here. Maybe you meant to use listFiles()?Haddock
@JimGarrison Yes it contains lots of sub-directories and files. Though it does not matter, because atleast it should return 2 directories - '.' and '..' .Austen
My guess is that there's a mismatch between the actual system-type and the system-type used by the client. This might cause the wrong FTPFileEntryParser implementation to be used. Take a look at FTPClientConfig and FTPClient#setParserFactory().Haddock
C
3

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);
Chao answered 19/7, 2015 at 18:46 Comment(0)
S
2

I think this may be an Apache Commons issue, it doesn't work for me, in fact it might not work because spaces are interpreted as delimiters for command parameters. I couldn't find a solution to your problem, all i can do is suggest you a workaround:

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

client.cwd("ABC XYZ");
FTPFile[] names = client.listDirectories(); //now this should work, it works for me
client.cdup();
FTPFile[] names2 = client.listDirectories("ABCXYZ"); //working

If you don't want to write this each time you have a directory with spaces in it's name, you can make a method that does it for you:

FTPFile[] listDirectories(String directory){
    if(directory.contains(" ")){
        client.cwd(directory);
        FTPFile[] listedDirectories = client.listDirectories();
        client.cdup();
        return listedDirectories;
    } else {
        return client.listDirectories(directory);
    }
}
Sully answered 12/1, 2013 at 10:28 Comment(0)
U
0

Apparently while listFiles(String path), have a problem with paths which include spaces, other functions does not have a similar problem. What you need to do is simply to change the working directory and than use listFiles().

Something like this:

private FTPFile[] getDirectoryFiles(String dirPath) throws IOException
{
    String cwd = ftp.printWorkingDirectory();

    ftp.changeWorkingDirectory(dirPath);
    FTPFile[] files = ftp.listFiles();
    ftp.changeWorkingDirectory(cwd);

    return files;               
}
Ulland answered 5/4, 2016 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.