mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge pull request #2574 from yebukong/v5-dev-yebukong
Ftp方法isDir和exist修复及改进
This commit is contained in:
commit
8b9d7b1486
@ -2,6 +2,7 @@ package cn.hutool.extra.ftp;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@ -73,7 +74,12 @@ public abstract class AbstractFtp implements Closeable {
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public boolean isDir(String dir) {
|
||||
return cd(dir);
|
||||
final String workDir = pwd();
|
||||
try {
|
||||
return cd(dir);
|
||||
} finally {
|
||||
cd(workDir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,8 +97,22 @@ public abstract class AbstractFtp implements Closeable {
|
||||
* @return 是否存在
|
||||
*/
|
||||
public boolean exist(String path) {
|
||||
if (StrUtil.isBlank(path)) {
|
||||
return false;
|
||||
}
|
||||
// 目录验证
|
||||
if (isDir(path)) {
|
||||
return true;
|
||||
}
|
||||
if (CharUtil.isFileSeparator(path.charAt(path.length() - 1))) {
|
||||
return false;
|
||||
}
|
||||
final String fileName = FileUtil.getName(path);
|
||||
final String dir = StrUtil.removeSuffix(path, fileName);
|
||||
if (".".equals(fileName) || "..".equals(fileName)) {
|
||||
return false;
|
||||
}
|
||||
// 文件验证
|
||||
final String dir = StrUtil.emptyToDefault(StrUtil.removeSuffix(path, fileName), ".");
|
||||
final List<String> names;
|
||||
try {
|
||||
names = ls(dir);
|
||||
|
@ -90,4 +90,63 @@ public class FtpTest {
|
||||
FileUtil.file("d:/test/download/" + name));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void isDirTest() throws Exception {
|
||||
try (Ftp ftp = new Ftp("127.0.0.1", 21)) {
|
||||
Console.log(ftp.pwd());
|
||||
ftp.isDir("/test");
|
||||
Console.log(ftp.pwd());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void existSftpTest() throws Exception {
|
||||
try (Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test")) {
|
||||
Console.log(ftp.pwd());
|
||||
Console.log(ftp.exist(null));
|
||||
Console.log(ftp.exist(""));
|
||||
Console.log(ftp.exist("."));
|
||||
Console.log(ftp.exist(".."));
|
||||
Console.log(ftp.exist("/"));
|
||||
Console.log(ftp.exist("a"));
|
||||
Console.log(ftp.exist("/home/test"));
|
||||
Console.log(ftp.exist("/home/test/"));
|
||||
Console.log(ftp.exist("/home/test//////"));
|
||||
Console.log(ftp.exist("/home/test/file1"));
|
||||
Console.log(ftp.exist("/home/test/file1/"));
|
||||
Console.log(ftp.exist("///////////"));
|
||||
Console.log(ftp.exist("./"));
|
||||
Console.log(ftp.exist("./file1"));
|
||||
Console.log(ftp.pwd());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void existFtpTest() throws Exception {
|
||||
try (Ftp ftp = new Ftp("127.0.0.1", 21)) {
|
||||
Console.log(ftp.pwd());
|
||||
Console.log(ftp.exist(null));
|
||||
Console.log(ftp.exist(""));
|
||||
Console.log(ftp.exist("."));
|
||||
Console.log(ftp.exist(".."));
|
||||
Console.log(ftp.exist("/"));
|
||||
Console.log(ftp.exist("a"));
|
||||
Console.log(ftp.exist("/test"));
|
||||
Console.log(ftp.exist("/test/"));
|
||||
Console.log(ftp.exist("/test//////"));
|
||||
Console.log(ftp.exist("/test/.."));
|
||||
Console.log(ftp.exist("/test/."));
|
||||
Console.log(ftp.exist("/file1"));
|
||||
Console.log(ftp.exist("/file1/"));
|
||||
Console.log(ftp.exist("///////////"));
|
||||
Console.log(ftp.exist("./"));
|
||||
Console.log(ftp.exist("./file1"));
|
||||
Console.log(ftp.exist("./2/3/4/.."));
|
||||
Console.log(ftp.pwd());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user