mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add isDir
This commit is contained in:
@@ -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);
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
*
|
||||
|
Reference in New Issue
Block a user