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

发布于 2024-10-09 01:45:08 字数 256 浏览 0 评论 0原文

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

我想做这样的事情:

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

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

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

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假情假意假温柔 2024-10-16 01:45:08

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

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

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

boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")
弱骨蛰伏 2024-10-16 01:45:08

您只需要检查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");
}

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");
}
嘴硬脾气大 2024-10-16 01:45:08

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

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
                        }
    }
}
}

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

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

酷到爆炸 2024-10-16 01:45:08

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

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

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);
}
凤舞天涯 2024-10-16 01:45:08

这是我查找给定路径是否代表目录或文件的方法。

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


}

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


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文