FTP接口增加rename方法,改文件/目录名

This commit is contained in:
厉军
2025-04-24 16:43:39 +08:00
parent 92d3eaeead
commit 1e9c92c015
7 changed files with 193 additions and 14 deletions

View File

@@ -250,6 +250,14 @@ public abstract class AbstractFtp implements Closeable {
*/
public abstract void recursiveDownloadFolder(String sourcePath, File destDir);
/**
* 重命名文件/目录
*
* @param from 原路径
* @param to 目标路径
*/
public abstract void rename(String from, String to);
// ---------------------------------------------------------------------------------------------------------------------------------------- Private method start
/**

View File

@@ -726,6 +726,25 @@ public class Ftp extends AbstractFtp {
}
}
/**
* 重命名文件/目录
*
* @param from 原路径
* @param to 目标路径
*
* @throws FtpException FTP异常
*/
@Override
public void rename(String from, String to) throws FtpException {
try {
if (!client.rename(from, to)) {
throw new FtpException("rename [{}] to [{}] fail", from, to);
}
} catch (IOException e) {
throw new FtpException(e);
}
}
/**
* 获取FTPClient客户端对象
*
@@ -745,4 +764,5 @@ public class Ftp extends AbstractFtp {
this.client = null;
}
}
}

View File

@@ -647,6 +647,23 @@ public class Sftp extends AbstractFtp {
}
/**
* 重命名文件/目录
*
* @param from 原路径
* @param to 目标路径
*
* @throws JschRuntimeException Jsch异常
*/
@Override
public void rename(String from, String to) throws JschRuntimeException {
try {
getClient().rename(from, to);
} catch (SftpException e) {
throw new JschRuntimeException(e);
}
}
/**
* 获取远程文件
*

View File

@@ -88,7 +88,7 @@ public class SshjSftp extends AbstractFtp {
* @param config FTP配置
* @since 5.3.3
*/
protected SshjSftp(FtpConfig config) {
public SshjSftp(FtpConfig config) {
super(config);
init();
}
@@ -210,6 +210,23 @@ public class SshjSftp extends AbstractFtp {
}
}
/**
* 重命名文件/目录
*
* @param from 原路径
* @param to 目标路径
*
* @throws FtpException FTP异常
*/
@Override
public void rename(String from, String to) throws FtpException {
try {
sftp.rename(from, to);
} catch (IOException e) {
throw new FtpException(e);
}
}
@Override
public void close() {
IoUtil.close(this.session);