Apache Commons FTPClient,检查远程目录是否存在并获取权限(linux - unix)

Xer*_*erg 7 java ftp ftp-client apache-commons-net

是否可以使用FTPClient(Apache commons-net)检查远程目录是否存在?

我想做这样的事情:

ftp.isDirectory(String path) //returns true, false
Run Code Online (Sandbox Code Playgroud)

然后获取目录的权限(chmod):

ftp.getPermisions(String path) //returns -rwxr-xr-x 
Run Code Online (Sandbox Code Playgroud)

小智 16

尝试将工作目录更改为您需要检查的目录:

boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")
Run Code Online (Sandbox Code Playgroud)


小智 5

你需要检查的只是 ftpClient.cwd("your Directory Name")

这将返回整数值

250 - 如果文件存在

要么

550 - 如果文件不存在

例如,

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");
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*aun 1

我也需要弄清楚这一点,但在玩了一会儿之后,我想我已经弄清楚了。我还没有测试过这个,但我认为它会成功

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
                        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

希望这有效/有帮助。这似乎也是一种相当多余的方法,因此可能有更好的方法。我不知道,我是这个库和 android 的新手