From e01ee4e01d9286273038c6c977fd1b766b267f78 Mon Sep 17 00:00:00 2001
From: Henry <2053622984@qq.com>
Date: Sun, 31 Aug 2025 17:08:34 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20ftp/sftp=E5=8A=9F=E8=83=BD=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=201.=20=E6=9B=B4=E6=8D=A2jsch=E4=BE=9D=E8=B5=96?=
=?UTF-8?q?=E5=BA=93=202.=20=E5=A2=9E=E5=8A=A0=E8=B6=85=E6=97=B6=E8=AE=BE?=
=?UTF-8?q?=E7=BD=AE=203.=20sftp=E5=88=9B=E5=BB=BA=E4=B8=8A=E5=B1=82?=
=?UTF-8?q?=E7=9B=AE=E5=BD=95=E6=97=B6=EF=BC=8C=E4=B8=8Eftp=E6=96=B9?=
=?UTF-8?q?=E5=BC=8F=E4=BF=9D=E6=8C=81=E4=B8=80=E8=87=B4=EF=BC=8C=E5=9B=A0?=
=?UTF-8?q?=E4=B8=BAhutool=E5=8C=85=E7=9A=84FileUtil.getParent=E5=9C=A8?=
=?UTF-8?q?=E4=B8=8D=E5=90=8C=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F=E4=B8=8A?=
=?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
yudao-dependencies/pom.xml | 4 ++--
.../file/core/client/ftp/FtpFileClient.java | 9 +++++---
.../file/core/client/sftp/SftpFileClient.java | 22 ++++++++++++++++---
3 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml
index ef203aeea..b2698eeb7 100644
--- a/yudao-dependencies/pom.xml
+++ b/yudao-dependencies/pom.xml
@@ -67,7 +67,7 @@
2.14.5
3.11.1
3.18.0
- 0.1.55
+ 2.27.3
3.2.2
2.7.0
3.0.6
@@ -576,7 +576,7 @@
${commons-net.version}
- com.jcraft
+ com.github.mwiede
jsch
${jsch.version}
diff --git a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java
index 4207eb7e1..6f3ac8648 100644
--- a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java
+++ b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java
@@ -27,8 +27,11 @@ public class FtpFileClient extends AbstractFileClient {
@Override
protected void doInit() {
// 初始化 Ftp 对象
- this.ftp = new Ftp(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(),
- CharsetUtil.CHARSET_UTF_8, null, null, FtpMode.valueOf(config.getMode()));
+ FtpConfig ftpConfig = new FtpConfig(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(),
+ CharsetUtil.CHARSET_UTF_8, null, null);
+ ftpConfig.setConnectionTimeout(3000L);
+ ftpConfig.setSoTimeout(10000L);
+ this.ftp = new Ftp(ftpConfig, FtpMode.valueOf(config.getMode()));
}
@Override
@@ -72,4 +75,4 @@ public class FtpFileClient extends AbstractFileClient {
ftp.reconnectIfTimeout();
}
-}
\ No newline at end of file
+}
diff --git a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java
index 000cbd10b..f577086c4 100644
--- a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java
+++ b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java
@@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.infra.framework.file.core.client.sftp;
import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.extra.ftp.FtpConfig;
import cn.hutool.extra.ssh.Sftp;
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
@@ -14,6 +16,11 @@ import java.io.File;
*/
public class SftpFileClient extends AbstractFileClient {
+ static {
+ // 某些旧的sftp服务器仅支持ssh-dss协议,该协议并不安全,默认不支持该协议,按需添加
+ JSch.setConfig("server_host_key", JSch.getConfig("server_host_key") + ",ssh-dss");
+ }
+
private Sftp sftp;
public SftpFileClient(Long id, SftpFileClientConfig config) {
@@ -23,17 +30,26 @@ public class SftpFileClient extends AbstractFileClient {
@Override
protected void doInit() {
// 初始化 Ftp 对象
- this.sftp = new Sftp(config.getHost(), config.getPort(), config.getUsername(), config.getPassword());
+ FtpConfig ftpConfig = new FtpConfig(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(),
+ CharsetUtil.CHARSET_UTF_8, null, null);
+ ftpConfig.setConnectionTimeout(3000L);
+ ftpConfig.setSoTimeout(10000L);
+ this.sftp = new Sftp(ftpConfig);
}
@Override
public String upload(byte[] content, String path, String type) {
// 执行写入
String filePath = getFilePath(path);
+ String fileName = FileUtil.getName(filePath);
+ String dir = StrUtil.removeSuffix(filePath, fileName);
File file = FileUtils.createTempFile(content);
reconnectIfTimeout();
- sftp.mkDirs(FileUtil.getParent(filePath, 1)); // 需要创建父目录,不然会报错
- sftp.upload(filePath, file);
+ sftp.mkDirs(dir); // 需要创建父目录,不然会报错
+ boolean success = sftp.upload(filePath, file);
+ if (!success) {
+ throw new JschRuntimeException(StrUtil.format("上传文件到目标目录 ({}) 失败", filePath));
+ }
// 拼接返回路径
return super.formatFileUrl(config.getDomain(), path);
}