From f572056d5a95145f744ddcf86384f39498b467ba Mon Sep 17 00:00:00 2001
From: looly
Date: Tue, 4 Jan 2022 07:06:47 +0800
Subject: [PATCH] add SshjSftp
---
CHANGELOG.md | 1 +
.../java/cn/hutool/extra/ssh/SshjSftp.java | 62 ++++++++++++-------
.../hutool/extra/{ftp => ssh}/SftpTest.java | 12 +++-
3 files changed, 50 insertions(+), 25 deletions(-)
rename hutool-extra/src/test/java/cn/hutool/extra/{ftp => ssh}/SftpTest.java (91%)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95674ce30..d9e8aaf9a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@
* 【db 】 优化Condition参数拆分(pr#2046@Github)
* 【core 】 优化ArrayUtil.isAllEmpty性能(pr#2045@Github)
* 【core 】 CharSequenceUtil.replace方法支持增补字符(pr#2041@Github)
+* 【extra 】 增加SshjSftp(pr#493@Gitee)
### 🐞Bug修复
* 【http 】 HttpUtil重定向次数失效问题(issue#I4O28Q@Gitee)
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 730bc051f..d458420c1 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,5 +1,6 @@
package cn.hutool.extra.ssh;
+import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
@@ -18,7 +19,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
-import java.util.stream.Collectors;
/**
* 在使用jsch 进行sftp协议下载文件时,总是中文乱码,而该框架源码又不允许设置编码。故:站在巨人的肩膀上,此类便孕育而出。
@@ -29,26 +29,54 @@ import java.util.stream.Collectors;
*
*
* @author youyongkun
- * @date 2021/12/31 14:51
- * @since 5.7.18
+ * @since 5.7.19
*/
public class SshjSftp extends AbstractFtp {
private SSHClient ssh;
private SFTPClient sftp;
+ /**
+ * 构造,使用默认端口
+ *
+ * @param sshHost 主机
+ */
public SshjSftp(String sshHost) {
this(new FtpConfig(sshHost, 22, null, null, CharsetUtil.CHARSET_UTF_8));
}
+ /**
+ * 构造
+ *
+ * @param sshHost 主机
+ * @param sshUser 用户名
+ * @param sshPass 密码
+ */
public SshjSftp(String sshHost, String sshUser, String sshPass) {
this(new FtpConfig(sshHost, 22, sshUser, sshPass, CharsetUtil.CHARSET_UTF_8));
}
+ /**
+ * 构造
+ *
+ * @param sshHost 主机
+ * @param sshPort 端口
+ * @param sshUser 用户名
+ * @param sshPass 密码
+ */
public SshjSftp(String sshHost, int sshPort, String sshUser, String sshPass) {
this(new FtpConfig(sshHost, sshPort, sshUser, sshPass, CharsetUtil.CHARSET_UTF_8));
}
+ /**
+ * 构造
+ *
+ * @param sshHost 主机
+ * @param sshPort 端口
+ * @param sshUser 用户名
+ * @param sshPass 密码
+ * @param charset 编码
+ */
public SshjSftp(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset) {
this(new FtpConfig(sshHost, sshPort, sshUser, sshPass, charset));
}
@@ -68,7 +96,6 @@ public class SshjSftp extends AbstractFtp {
* SSH 初始化并创建一个sftp客户端.
*
* @author youyongkun
- * @date 2021/12/31 14:59
* @since 5.7.18
*/
public void init() {
@@ -115,22 +142,23 @@ public class SshjSftp extends AbstractFtp {
public boolean mkdir(String dir) {
try {
sftp.mkdir(dir);
- return containsFile(dir);
} catch (IOException e) {
throw new FtpException(e);
}
+ return containsFile(dir);
}
@Override
public List ls(String path) {
+ List infoList;
try {
- List infoList = sftp.ls(path);
- if (infoList != null && infoList.size() > 0) {
- return infoList.stream().map(info -> info.getName()).collect(Collectors.toList());
- }
+ infoList = sftp.ls(path);
} catch (IOException e) {
throw new FtpException(e);
}
+ if (CollUtil.isNotEmpty(infoList)) {
+ return CollUtil.map(infoList, RemoteResourceInfo::getName, true);
+ }
return null;
}
@@ -177,9 +205,7 @@ public class SshjSftp extends AbstractFtp {
public void recursiveDownloadFolder(String sourcePath, File destDir) {
List files = ls(sourcePath);
if (files != null && !files.isEmpty()) {
- files.forEach(path -> {
- download(sourcePath + "/" + path, destDir);
- });
+ files.forEach(path -> download(sourcePath + "/" + path, destDir));
}
}
@@ -199,7 +225,6 @@ public class SshjSftp extends AbstractFtp {
* @param fileDir 文件绝对路径
* @return true:包含 false:不包含
* @author youyongkun
- * @date 2021/12/31 15:36
* @since 5.7.18
*/
public boolean containsFile(String fileDir) {
@@ -218,8 +243,7 @@ public class SshjSftp extends AbstractFtp {
* @param exec 命令
* @return 返回响应结果.
* @author youyongkun
- * @date 2021/12/31 15:59
- * @since 5.7.18
+ * @since 5.7.19
*/
public String command(String exec) {
Session session = null;
@@ -231,13 +255,7 @@ public class SshjSftp extends AbstractFtp {
} catch (Exception e) {
throw new FtpException(e);
} finally {
- if (session != null) {
- try {
- session.close();
- } catch (Exception e) {
- throw new FtpException(e);
- }
- }
+ IoUtil.close(session);
}
}
}
diff --git a/hutool-extra/src/test/java/cn/hutool/extra/ftp/SftpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java
similarity index 91%
rename from hutool-extra/src/test/java/cn/hutool/extra/ftp/SftpTest.java
rename to hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java
index 4a91f0518..1de875877 100644
--- a/hutool-extra/src/test/java/cn/hutool/extra/ftp/SftpTest.java
+++ b/hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java
@@ -1,8 +1,8 @@
-package cn.hutool.extra.ftp;
+package cn.hutool.extra.ssh;
import cn.hutool.core.util.CharsetUtil;
-import cn.hutool.extra.ssh.SshjSftp;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
@@ -12,7 +12,6 @@ import java.util.List;
* 基于sshj 框架SFTP 封装测试.
*
* @author youyongkun
- * @date 2021/12/31 16:11
* @since 5.7.18
*/
public class SftpTest {
@@ -20,11 +19,13 @@ public class SftpTest {
private SshjSftp sshjSftp;
@Before
+ @Ignore
public void init() {
sshjSftp = new SshjSftp("ip", 22, "test", "test", CharsetUtil.CHARSET_UTF_8);
}
@Test
+ @Ignore
public void lsTest() {
List files = sshjSftp.ls("/");
if (files != null && !files.isEmpty()) {
@@ -33,28 +34,33 @@ public class SftpTest {
}
@Test
+ @Ignore
public void downloadTest() {
sshjSftp.recursiveDownloadFolder("/home/test/temp", new File("C:\\Users\\akwangl\\Downloads\\temp"));
}
@Test
+ @Ignore
public void uploadTest() {
sshjSftp.upload("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME"));
}
@Test
+ @Ignore
public void mkDirTest() {
boolean flag = sshjSftp.mkdir("/home/test/temp");
System.out.println("是否创建成功: " + flag);
}
@Test
+ @Ignore
public void mkDirsTest() {
// 在当前目录下批量创建目录
sshjSftp.mkDirs("/home/test/temp");
}
@Test
+ @Ignore
public void delDirTest() {
sshjSftp.delDir("/home/test/temp");
}