FTPClient(Apache commons-net)是否可以检查远程目录是否存在?
我想做这样的事情:
ftp.isDirectory(String path) //returns true, false
然后获取目录的权限(chmod):
ftp.getPermisions(String path) //returns -rwxr-xr-x
答案 0 :(得分:15)
尝试将工作目录更改为您需要检查的目录:
boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")
答案 1 :(得分:5)
您只需要检查ftpClient.cwd("your Directory Name")
这将返回整数值
250
- 如果文件存在
OR
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");
}
答案 2 :(得分: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
}
}
}
}
希望这有效/有帮助。这似乎是一种非常冗余的方式,因此可能有更好的方法。 Idk,我是这个库和android的新手
答案 3 :(得分:0)
如果远程主机支持它,最简单的方法是mlistFile()。
if (ftpClient.featureValue("MLST") != null) {
FTPFile file = ftpClient.mlistFile(null);
boolean b = file.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION);
}