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

22,866

Solution 1

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

boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")

Solution 2

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");
}

Solution 3

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

Share:
22,866

Related videos on Youtube

Xerg
Author by

Xerg

s

Updated on November 19, 2020

Comments

  • Xerg
    Xerg over 3 years

    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 
    
  • MaddHacker
    MaddHacker about 12 years
    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.
  • Javo
    Javo about 6 years
    The CWD command means - Change Working Directory btw, so its not really different from boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")
  • Shalin Patel
    Shalin Patel about 6 years
    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.
  • Aura
    Aura almost 5 years
    If you send CWD, you will lost last working directory. Sometimes it would be the problem when handle multi-thread situation.

Related