add isDir

This commit is contained in:
Looly 2021-07-18 22:10:46 +08:00
parent 0915b8918d
commit a081f999b5
7 changed files with 52 additions and 15 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.5 (2021-07-16)
# 5.7.5 (2021-07-18)
### 🐣新特性
* 【core 】 DateUtil增加ceiling重载可选是否归零毫秒
@ -14,6 +14,7 @@
* 【core 】 Calculator类支持取模运算issue#I40DUW@Gitee
* 【core 】 增加Base64.isBase64方法issue#1710@Github
* 【core 】 ManifestUtil新增方法getManifest(Class<?> cls)pr#370@Gitee
* 【extra 】 AbstractFtp增加isDir方法issue#1716@Github
### 🐞Bug修复
* 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee

View File

@ -1479,9 +1479,9 @@ public class FileUtil extends PathUtil {
}
// issue#1703@Github
if(tops > 0 && StrUtil.isEmpty(prefix)){
if (tops > 0 && StrUtil.isEmpty(prefix)) {
// 只有相对路径补充开头的..绝对路径直接忽略之
while (tops-- > 0){
while (tops-- > 0) {
//遍历完节点发现还有上级标注即开头有一个或多个..补充之
// Normal path element found.
pathElements.add(0, StrUtil.DOUBLE_DOT);
@ -1555,7 +1555,11 @@ public class FileUtil extends PathUtil {
}
/**
* 返回文件名
* 返回文件名<br>
* <pre>
* "d:/test/aaa" 返回 "aaa"
* "/test/aaa.jpg" 返回 "aaa.jpg"
* </pre>
*
* @param filePath 文件
* @return 文件名

View File

@ -57,7 +57,11 @@ public class FileNameUtil {
}
/**
* 返回文件名
* 返回文件名<br>
* <pre>
* "d:/test/aaa" 返回 "aaa"
* "/test/aaa.jpg" 返回 "aaa.jpg"
* </pre>
*
* @param filePath 文件
* @return 文件名

View File

@ -2163,7 +2163,8 @@ public class NumberUtil {
} catch (Exception ignore) {
// 忽略解析错误
}
return StrUtil.isBlank(number) ? BigDecimal.ZERO : new BigDecimal(number);
return StrUtil.isBlank(number) ? BigDecimal.ZERO : new
BigDecimal(number);
}
/**

View File

@ -65,6 +65,17 @@ public abstract class AbstractFtp implements Closeable {
*/
public abstract String pwd();
/**
* 判断给定路径是否为目录
*
* @param dir 被判断的路径
* @return 是否为目录
* @since 5.7.5
*/
public boolean isDir(String dir) {
return cd(dir);
}
/**
* 在当前远程目录工作目录下创建新的目录
*
@ -83,9 +94,9 @@ public abstract class AbstractFtp implements Closeable {
final String fileName = FileUtil.getName(path);
final String dir = StrUtil.removeSuffix(path, fileName);
final List<String> names;
try{
try {
names = ls(dir);
} catch (FtpException ignore){
} catch (FtpException ignore) {
return false;
}
return containsIgnoreCase(names, fileName);
@ -131,14 +142,14 @@ public abstract class AbstractFtp implements Closeable {
for (String s : dirs) {
if (StrUtil.isNotEmpty(s)) {
boolean exist = true;
try{
try {
if (false == cd(s)) {
exist = false;
}
} catch (FtpException e){
} catch (FtpException e) {
exist = false;
}
if(false == exist){
if (false == exist) {
//目录不存在时创建
mkdir(s);
cd(s);

View File

@ -355,7 +355,7 @@ public class Ftp extends AbstractFtp {
String pwd = null;
if (StrUtil.isNotBlank(path)) {
pwd = pwd();
if (false == cd(path)) {
if (false == isDir(path)) {
throw new FtpException("Change dir to [{}] error, maybe path not exist!", path);
}
}
@ -419,7 +419,7 @@ public class Ftp extends AbstractFtp {
final String pwd = pwd();
final String fileName = FileUtil.getName(path);
final String dir = StrUtil.removeSuffix(path, fileName);
if (false == cd(dir)) {
if (false == isDir(dir)) {
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
}
@ -537,7 +537,7 @@ public class Ftp extends AbstractFtp {
if (StrUtil.isNotBlank(path)) {
mkDirs(path);
if (false == cd(path)) {
if (false == isDir(path)) {
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
}
}
@ -645,7 +645,7 @@ public class Ftp extends AbstractFtp {
pwd = pwd();
}
if (false == cd(path)) {
if (false == isDir(path)) {
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
}

View File

@ -12,6 +12,7 @@ import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.ChannelSftp.LsEntrySelector;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.SftpProgressMonitor;
@ -318,6 +319,10 @@ public class Sftp extends AbstractFtp {
@Override
public boolean mkdir(String dir) {
if(isDir(dir)){
// 目录已经存在创建直接返回
return true;
}
try {
this.channel.mkdir(dir);
return true;
@ -326,6 +331,17 @@ public class Sftp extends AbstractFtp {
}
}
@Override
public boolean isDir(String dir){
final SftpATTRS sftpATTRS;
try {
sftpATTRS = this.channel.stat(dir);
} catch (SftpException e) {
throw new FtpException(e);
}
return sftpATTRS.isDir();
}
/**
* 打开指定目录如果指定路径非目录或不存在返回false
*