From 1e9c92c015d1a284e47d627f95b35ca0bb833a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8E=89=E5=86=9B?= Date: Thu, 24 Apr 2025 16:43:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?FTP=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0rena?= =?UTF-8?q?me=E6=96=B9=E6=B3=95=EF=BC=8C=E6=94=B9=E6=96=87=E4=BB=B6/?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/extra/ftp/AbstractFtp.java | 8 ++ .../main/java/cn/hutool/extra/ftp/Ftp.java | 20 +++++ .../main/java/cn/hutool/extra/ssh/Sftp.java | 17 ++++ .../java/cn/hutool/extra/ssh/SshjSftp.java | 19 +++- .../java/cn/hutool/extra/ftp/FtpTest.java | 12 +++ .../java/cn/hutool/extra/ssh/SftpTest.java | 45 +++++++--- .../cn/hutool/extra/ssh/SshjSftpTest.java | 86 +++++++++++++++++++ 7 files changed, 193 insertions(+), 14 deletions(-) create mode 100644 hutool-extra/src/test/java/cn/hutool/extra/ssh/SshjSftpTest.java diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java index bdf695605..0bdd1cb95 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java @@ -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 /** 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 96fbd275e..c9289cd5d 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 @@ -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; } } + } 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 412509098..9423a64a3 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 @@ -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); + } + } + /** * 获取远程文件 * 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 617d11da3..c8468cec4 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 @@ -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); 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/SftpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java index e7429b873..f4c25a33c 100644 --- a/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java @@ -1,33 +1,34 @@ package cn.hutool.extra.ssh; import cn.hutool.core.util.CharsetUtil; -import org.junit.Before; +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.*; + /** * 基于sshj 框架SFTP 封装测试. * * @author youyongkun * @since 5.7.18 */ -public class SftpTest { +class SftpTest { - private SshjSftp sshjSftp; + private static Sftp sftp; - @Before - @Disabled - public void init() { - sshjSftp = new SshjSftp("ip", 22, "test", "test", CharsetUtil.CHARSET_UTF_8); + @BeforeAll + public static void init() { + sftp = new Sftp("localhost", 22, "test", "test", CharsetUtil.CHARSET_UTF_8); } @Test @Disabled public void lsTest() { - List files = sshjSftp.ls("/"); + List files = sftp.ls("/"); if (files != null && !files.isEmpty()) { files.forEach(System.out::print); } @@ -36,19 +37,19 @@ public class SftpTest { @Test @Disabled public void downloadTest() { - sshjSftp.recursiveDownloadFolder("/home/test/temp", new File("C:\\Users\\akwangl\\Downloads\\temp")); + sftp.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")); + sftp.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"); + boolean flag = sftp.mkdir("/home/test/temp"); System.out.println("是否创建成功: " + flag); } @@ -56,12 +57,30 @@ public class SftpTest { @Disabled public void mkDirsTest() { // 在当前目录下批量创建目录 - sshjSftp.mkDirs("/home/test/temp"); + sftp.mkDirs("/home/test/temp"); } @Test @Disabled public void delDirTest() { - sshjSftp.delDir("/home/test/temp"); + sftp.delDir("/home/test/temp"); + } + + @Test + public void pwdTest() { +// mkDirsTest(); + sftp.mkdir("/ftp"); + sftp.cd("/ftp"); + String pwd = sftp.pwd(); + System.out.println("当前目录: " + pwd); + assertEquals("/ftp", pwd); + } + + @Test + public void renameTest() { + sftp.mkdir("/ftp-1"); + assertTrue(sftp.exist("/ftp-1")); + sftp.rename("/ftp-1", "/ftp-2"); + assertTrue(sftp.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")); + } +} From 81159f75bb7dc7821decf83a9daa68f3b4da1246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8E=89=E5=86=9B?= Date: Wed, 30 Apr 2025 15:20:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3pwd=E3=80=81cd=E8=B0=83?= =?UTF-8?q?=E7=94=A8command=E5=AF=BC=E8=87=B4=E4=BB=85SftpSubsystem?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=97=B6=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/extra/ssh/SshjSftp.java | 78 ++++++++++++++----- 1 file changed, 60 insertions(+), 18 deletions(-) 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 9f6c47e3b..e3217d11a 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 @@ -1,6 +1,7 @@ package cn.hutool.extra.ssh; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; @@ -36,6 +37,7 @@ public class SshjSftp extends AbstractFtp { private SSHClient ssh; private SFTPClient sftp; private Session session; + private String workingDir; /** * 构造,使用默认端口 @@ -126,34 +128,65 @@ public class SshjSftp extends AbstractFtp { return this; } + private String getPath(String path) { + if (StrUtil.isBlank(this.workingDir)) { + try { + this.workingDir = sftp.canonicalize(""); + } catch (IOException e) { + throw new FtpException(e); + } + } + + if (StrUtil.isBlank(path)) { + return this.workingDir; + } + + // 如果是绝对路径,则返回 + if (StrUtil.startWith(path, StrUtil.SLASH)) { + return path; + } else { + String tmp = StrUtil.removeSuffix(this.workingDir, StrUtil.SLASH); + return StrUtil.format("{}/{}", tmp, path); + } + } + + /** + * 改变目录,注意目前不支持.. + * @param directory directory + * @return true:成功 + */ @Override public boolean cd(String directory) { - String exec = String.format("cd %s", directory); - command(exec); - String pwd = pwd(); - return pwd.equals(directory); + String newPath = getPath(directory); + try { + sftp.ls(newPath); + this.workingDir = newPath; + return true; + } catch (IOException e) { + throw new FtpException(e); + } } @Override public String pwd() { - return command("pwd"); + return getPath(null); } @Override public boolean mkdir(String dir) { try { - sftp.mkdir(dir); + sftp.mkdir(getPath(dir)); } catch (IOException e) { throw new FtpException(e); } - return containsFile(dir); + return containsFile(getPath(dir)); } @Override public List ls(String path) { List infoList; try { - infoList = sftp.ls(path); + infoList = sftp.ls(getPath(path)); } catch (IOException e) { throw new FtpException(e); } @@ -166,8 +199,8 @@ public class SshjSftp extends AbstractFtp { @Override public boolean delFile(String path) { try { - sftp.rm(path); - return !containsFile(path); + sftp.rm(getPath(path)); + return !containsFile(getPath(path)); } catch (IOException e) { throw new FtpException(e); } @@ -176,8 +209,8 @@ public class SshjSftp extends AbstractFtp { @Override public boolean delDir(String dirPath) { try { - sftp.rmdir(dirPath); - return !containsFile(dirPath); + sftp.rmdir(getPath(dirPath)); + return !containsFile(getPath(dirPath)); } catch (IOException e) { throw new FtpException(e); } @@ -186,8 +219,11 @@ public class SshjSftp extends AbstractFtp { @Override public boolean upload(String destPath, File file) { try { - sftp.put(new FileSystemFile(file), destPath); - return containsFile(destPath); + if (StrUtil.endWith(destPath, StrUtil.SLASH)) { + destPath += file.getName(); + } + sftp.put(new FileSystemFile(file), getPath(destPath)); + return containsFile(getPath(destPath)); } catch (IOException e) { throw new FtpException(e); } @@ -196,7 +232,7 @@ public class SshjSftp extends AbstractFtp { @Override public void download(String path, File outFile) { try { - sftp.get(path, new FileSystemFile(outFile)); + sftp.get(getPath(path), new FileSystemFile(outFile)); } catch (IOException e) { throw new FtpException(e); } @@ -204,9 +240,15 @@ public class SshjSftp extends AbstractFtp { @Override public void recursiveDownloadFolder(String sourcePath, File destDir) { - List files = ls(sourcePath); + if (!destDir.exists() || !destDir.isDirectory()) { + if (!destDir.mkdirs()) { + throw new FtpException("创建目录" + destDir.getAbsolutePath() + "失败"); + } + } + + List files = ls(getPath(sourcePath)); if (files != null && !files.isEmpty()) { - files.forEach(path -> download(sourcePath + "/" + path, destDir)); + files.forEach(file -> download(sourcePath + "/" + file, FileUtil.file(destDir, file))); } } @@ -236,7 +278,7 @@ public class SshjSftp extends AbstractFtp { */ public boolean containsFile(String fileDir) { try { - sftp.lstat(fileDir); + sftp.lstat(getPath(fileDir)); return true; } catch (IOException e) { return false; From 6e2e137b91494e6861f282e04e8e4e829de21b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8E=89=E5=86=9B?= Date: Wed, 30 Apr 2025 15:28:18 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/extra/ssh/SftpTest.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java index e7429b873..81f0669ae 100644 --- a/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java @@ -2,8 +2,8 @@ package cn.hutool.extra.ssh; import cn.hutool.core.util.CharsetUtil; import org.junit.Before; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Disabled; import java.io.File; import java.util.List; @@ -21,7 +21,7 @@ public class SftpTest { @Before @Disabled public void init() { - sshjSftp = new SshjSftp("ip", 22, "test", "test", CharsetUtil.CHARSET_UTF_8); + sshjSftp = new SshjSftp("127.0.0.1", 8022, "test", "test", CharsetUtil.CHARSET_UTF_8); } @Test @@ -29,39 +29,53 @@ public class SftpTest { public void lsTest() { List files = sshjSftp.ls("/"); if (files != null && !files.isEmpty()) { - files.forEach(System.out::print); + files.forEach(System.out::println); } } @Test @Disabled public void downloadTest() { - sshjSftp.recursiveDownloadFolder("/home/test/temp", new File("C:\\Users\\akwangl\\Downloads\\temp")); + sshjSftp.recursiveDownloadFolder("/temp/20250427/", new File("D:\\temp\\20250430\\20250427\\")); } @Test @Disabled public void uploadTest() { - sshjSftp.upload("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME")); + sshjSftp.upload("/ftp-2/20250430/", new File("D:\\temp\\20250430\\test.txt")); } @Test @Disabled public void mkDirTest() { - boolean flag = sshjSftp.mkdir("/home/test/temp"); + boolean flag = sshjSftp.mkdir("/ftp-2/20250430-1"); System.out.println("是否创建成功: " + flag); } + @Test + @Disabled + public void pwdTest() { + String pwd = sshjSftp.pwd(); + System.out.println("PWD: " + pwd); + } + @Test @Disabled public void mkDirsTest() { // 在当前目录下批量创建目录 - sshjSftp.mkDirs("/home/test/temp"); + sshjSftp.mkDirs("/ftp-2/20250430-2/t1/t2/"); } @Test @Disabled public void delDirTest() { - sshjSftp.delDir("/home/test/temp"); + sshjSftp.delDir("/ftp-2/20250430-2/t1/t2"); + } + + @Test + @Disabled + public void cdTest() { + System.out.println(sshjSftp.cd("/ftp-2")); + System.out.println(sshjSftp.cd("/ftp-4")); } } From 2b777c3841fcd121dd984404abeafab2bf3b118a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8E=89=E5=86=9B?= Date: Wed, 30 Apr 2025 16:22:14 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=87=8D=E5=A4=8Drename?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95SshjSftpTest=EF=BC=8CSftpTest?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=B5=8B=E8=AF=95Sftp=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/extra/ftp/Ftp.java | 19 ---------------- .../main/java/cn/hutool/extra/ssh/Sftp.java | 17 -------------- .../java/cn/hutool/extra/ssh/SshjSftp.java | 17 -------------- .../java/cn/hutool/extra/ssh/SftpTest.java | 22 +++++++++---------- .../cn/hutool/extra/ssh/SshjSftpTest.java | 6 ++--- 5 files changed, 14 insertions(+), 67 deletions(-) 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 0ca46df71..ee4aab39e 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,25 +737,6 @@ 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客户端对象 * 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 aee22386a..9f47866fd 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,23 +656,6 @@ 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 4d8b0c040..227f4e421 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 @@ -261,23 +261,6 @@ 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/ssh/SftpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java index 81f0669ae..39eaeca3d 100644 --- a/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java @@ -16,18 +16,18 @@ import java.util.List; */ public class SftpTest { - private SshjSftp sshjSftp; + private Sftp sftp; @Before @Disabled public void init() { - sshjSftp = new SshjSftp("127.0.0.1", 8022, "test", "test", CharsetUtil.CHARSET_UTF_8); + sftp = new Sftp("127.0.0.1", 8022, "test", "test", CharsetUtil.CHARSET_UTF_8); } @Test @Disabled public void lsTest() { - List files = sshjSftp.ls("/"); + List files = sftp.ls("/"); if (files != null && !files.isEmpty()) { files.forEach(System.out::println); } @@ -36,26 +36,26 @@ public class SftpTest { @Test @Disabled public void downloadTest() { - sshjSftp.recursiveDownloadFolder("/temp/20250427/", new File("D:\\temp\\20250430\\20250427\\")); + sftp.recursiveDownloadFolder("/temp/20250427/", new File("D:\\temp\\20250430\\20250427\\")); } @Test @Disabled public void uploadTest() { - sshjSftp.upload("/ftp-2/20250430/", new File("D:\\temp\\20250430\\test.txt")); + sftp.upload("/ftp-2/20250430/", new File("D:\\temp\\20250430\\test.txt")); } @Test @Disabled public void mkDirTest() { - boolean flag = sshjSftp.mkdir("/ftp-2/20250430-1"); + boolean flag = sftp.mkdir("/ftp-2/20250430-1"); System.out.println("是否创建成功: " + flag); } @Test @Disabled public void pwdTest() { - String pwd = sshjSftp.pwd(); + String pwd = sftp.pwd(); System.out.println("PWD: " + pwd); } @@ -63,19 +63,19 @@ public class SftpTest { @Disabled public void mkDirsTest() { // 在当前目录下批量创建目录 - sshjSftp.mkDirs("/ftp-2/20250430-2/t1/t2/"); + sftp.mkDirs("/ftp-2/20250430-2/t1/t2/"); } @Test @Disabled public void delDirTest() { - sshjSftp.delDir("/ftp-2/20250430-2/t1/t2"); + sftp.delDir("/ftp-2/20250430-2/t1/t2"); } @Test @Disabled public void cdTest() { - System.out.println(sshjSftp.cd("/ftp-2")); - System.out.println(sshjSftp.cd("/ftp-4")); + System.out.println(sftp.cd("/ftp-2")); + System.out.println(sftp.cd("/ftp-4")); } } 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 index 6c5c8ef63..29ff13358 100644 --- a/hutool-extra/src/test/java/cn/hutool/extra/ssh/SshjSftpTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SshjSftpTest.java @@ -23,7 +23,7 @@ class SshjSftpTest { @BeforeAll public static void init() { - sshjSftp = new SshjSftp("localhost", 22, "test", "test", CharsetUtil.CHARSET_UTF_8); + sshjSftp = new SshjSftp("localhost", 8022, "test", "test", CharsetUtil.CHARSET_UTF_8); } @Test @@ -31,7 +31,7 @@ class SshjSftpTest { public void lsTest() { List files = sshjSftp.ls("/"); if (files != null && !files.isEmpty()) { - files.forEach(System.out::print); + files.forEach(System.out::println); } } @@ -78,7 +78,7 @@ class SshjSftpTest { @Test public void renameTest() { - sshjSftp.mkdir("/ftp-1"); +// sshjSftp.mkdir("/ftp-1"); assertTrue(sshjSftp.exist("/ftp-1")); sshjSftp.rename("/ftp-1", "/ftp-2"); assertTrue(sshjSftp.exist("/ftp-2"));