diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java index 21cc8368b..0ca46df71 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java @@ -737,6 +737,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客户端对象 * @@ -756,4 +775,5 @@ public class Ftp extends AbstractFtp { this.client = null; } } + } diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java index 9f47866fd..aee22386a 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java @@ -656,6 +656,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); + } + } + /** * 获取远程文件 * diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java index e3217d11a..4d8b0c040 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java @@ -90,7 +90,7 @@ public class SshjSftp extends AbstractFtp { * @param config FTP配置 * @since 5.3.3 */ - protected SshjSftp(FtpConfig config) { + public SshjSftp(FtpConfig config) { super(config); init(); } @@ -261,6 +261,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); diff --git a/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java index 33f392519..84692a4a1 100644 --- a/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java @@ -13,6 +13,8 @@ import java.io.File; import java.io.IOException; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class FtpTest { @Test @@ -171,4 +173,14 @@ public class FtpTest { Console.log(ftp.pwd()); } } + + @Test + public void renameTest() { + final Ftp ftp = new Ftp("localhost", 21, "test", "test"); + + ftp.mkdir("/ftp-1"); + assertTrue(ftp.exist("/ftp-1")); + ftp.rename("/ftp-1", "/ftp-2"); + assertTrue(ftp.exist("/ftp-2")); + } } diff --git a/hutool-extra/src/test/java/cn/hutool/extra/ssh/SshjSftpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SshjSftpTest.java new file mode 100644 index 000000000..6c5c8ef63 --- /dev/null +++ b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SshjSftpTest.java @@ -0,0 +1,86 @@ +package cn.hutool.extra.ssh; + +import cn.hutool.core.util.CharsetUtil; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * 基于sshj 框架SFTP 封装测试. + * + * @author youyongkun + * @since 5.7.18 + */ +class SshjSftpTest { + + private static SshjSftp sshjSftp; + + @BeforeAll + public static void init() { + sshjSftp = new SshjSftp("localhost", 22, "test", "test", CharsetUtil.CHARSET_UTF_8); + } + + @Test + @Disabled + public void lsTest() { + List files = sshjSftp.ls("/"); + if (files != null && !files.isEmpty()) { + files.forEach(System.out::print); + } + } + + @Test + @Disabled + public void downloadTest() { + sshjSftp.recursiveDownloadFolder("/home/test/temp", new File("C:\\Users\\akwangl\\Downloads\\temp")); + } + + @Test + @Disabled + public void uploadTest() { + sshjSftp.upload("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME")); + } + + @Test + @Disabled + public void mkDirTest() { + boolean flag = sshjSftp.mkdir("/home/test/temp"); + System.out.println("是否创建成功: " + flag); + } + + @Test + @Disabled + public void mkDirsTest() { + // 在当前目录下批量创建目录 + sshjSftp.mkDirs("/home/test/temp"); + } + + @Test + @Disabled + public void delDirTest() { + sshjSftp.delDir("/home/test/temp"); + } + + @Test + public void pwdTest() { +// mkDirsTest(); + sshjSftp.cd("/ftp"); + String pwd = sshjSftp.pwd(); + System.out.println("当前目录: " + pwd); + assertEquals("/ftp", pwd); + } + + @Test + public void renameTest() { + sshjSftp.mkdir("/ftp-1"); + assertTrue(sshjSftp.exist("/ftp-1")); + sshjSftp.rename("/ftp-1", "/ftp-2"); + assertTrue(sshjSftp.exist("/ftp-2")); + } +}