Apache Commons FTPClient, check if remote directory exist and get permissions (linux - unix)
Asked Answered
E

5

8

Is it possible with FTPClient (Apache commons-net) to check if a remote directory exists?

I want to do something like this:

ftp.isDirectory(String path) //returns true, false

And then get permissions (chmod) of directory:

ftp.getPermisions(String path) //returns -rwxr-xr-x 
Epiphenomenon answered 21/12, 2010 at 14:42 Comment(0)
A
17

Try to change the working directory to the directory you need to check:

boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")
Ally answered 1/7, 2011 at 8:22 Comment(1)
This seems to be the only way to really do this, unfortunately. I think it's just more the limitations of FTP than the client.Coated
G
7

What you just Need to check is just ftpClient.cwd("your Directory Name")

this will return you integer values

250 - If File Exists

OR

550 - If File Doesn't Exist

For Example,

if(ftpClient.cwd(uploadDirectoryPath)==550){
     System.out.println("Directory Doesn't Exists");
}else if(ftpClient.cwd(uploadDirectoryPath)==250){
     System.out.println("Directory Exists");
}else{
     System.out.println("Unknown Status");
}
Gilbreath answered 18/11, 2016 at 21:41 Comment(4)
The CWD command means - Change Working Directory btw, so its not really different from boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")Nuncio
Yes, it is same but I gave this example as there is some other status also which we can handle. And we can have more control over our coding that's why.Gilbreath
If you send CWD, you will lost last working directory. Sometimes it would be the problem when handle multi-thread situation.Clausewitz
@Clausewitz yes exactly, happened with me I don't want to change last working directory.Giselle
K
1

I needed to figure this out too, but after doing a little play, I think I figured it out. I havent gotten to test this yet, but I think it will do the trik

FTPFile file[];
file = new FTPFile[ftpClient.listFiles().length];
for (int i = 0; i<file.length; i++) {
if (file[i].getName() == "directory name") {
    if (file[i].isDirectory()) {
    //Do stuff if it is a directory here
         if (file[i].hasPermission(access, permission) {
        //Do whatever you want with permissions - access and permission arguments are int's
                        }
    }
}
}

Hope this works/helps. This also seems like a pretty redundant way, so there may be a better way of doing it. Idk, im new to this library and android

Kendre answered 21/3, 2011 at 22:35 Comment(0)
B
0

If the remote host supports it, the simplest method is mlistFile().

if (ftpClient.featureValue("MLST") != null) {
    FTPFile file = ftpClient.mlistFile(null);
    boolean b = file.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION);
}
Barred answered 6/10, 2014 at 14:56 Comment(0)
O
0

Here is my take on to find whether given path represents a directory or a file.

public static boolean isFtpPathDirectory(String file_path)
{
        try (InputStream inputStream=ftpClient.retrieveFileStream(file_path))
        {
            return inputStream == null;
        } catch (IOException e) {
            return false;
        }


}
Opine answered 12/3, 2023 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.