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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4500425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 18:05:17  来源:igfitidea点击:

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

javaftpftp-clientapache-commons-net

提问by Xerg

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

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

I want to do something like this:

我想做这样的事情:

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

And then get permissions (chmod) of directory:

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

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

回答by Shaun

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

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

回答by Juris

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

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

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

回答by jaco0646

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

如果远程主机支持,最简单的方法是mlistFile()

if (ftpClient.featureValue("MLST") != null) {
    FTPFile file = ftpClient.mlistFile(null);
    boolean b = file.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION);
}

回答by Shalin Patel

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

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

this will return you integer values

这将返回整数值

250- If File Exists

250- 如果文件存在

OR

或者

550- If File Doesn't Exist

550- 如果文件不存在

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