/**
* 创建目录
* @param createpath
* @return
*/
public static boolean createDir(String createpath, ChannelSftp sftp) {
try
{
if (isDirExist(createpath,sftp)) {
sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString(),sftp)) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}
}
sftp.cd(createpath);
return true;
}
catch (SftpException e) {
e.printStackTrace();
}
return false;
}
/**
* 判断目录是否存在
* @param directory
* @return
*/
public static boolean isDirExist(String directory, ChannelSftp sftp)
{
boolean isDirExistFlag = false;
try
{
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
}
catch (Exception e)
{
if (e.getMessage().toLowerCase().equals("no such file"))
{
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
SFTP创建目录和判断目录是否存在
最新推荐文章于 2025-03-04 10:22:58 发布