开发者

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

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 开发者_JAVA技巧true, false

And then get permissions (chmod) of directory:

ftp.getPermisions(String path) //returns -rwxr-xr-x 


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

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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜