diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml
index 0045d4e30..3e4d2c551 100644
--- a/yudao-dependencies/pom.xml
+++ b/yudao-dependencies/pom.xml
@@ -69,7 +69,7 @@
2.14.5
3.11.1
3.18.0
- 0.1.55
+ 2.27.3
2.9.3
2.7.0
3.0.6
@@ -591,7 +591,7 @@
${commons-net.version}
- com.jcraft
+ com.github.mwiede
jsch
${jsch.version}
diff --git a/yudao-module-infra/yudao-module-infra-server/pom.xml b/yudao-module-infra/yudao-module-infra-server/pom.xml
index 45bb41460..1d56fbe66 100644
--- a/yudao-module-infra/yudao-module-infra-server/pom.xml
+++ b/yudao-module-infra/yudao-module-infra-server/pom.xml
@@ -129,7 +129,7 @@
commons-net
- com.jcraft
+ com.github.mwiede
jsch
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..93bff27ec 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
@@ -4,6 +4,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ftp.Ftp;
+import cn.hutool.extra.ftp.FtpConfig;
import cn.hutool.extra.ftp.FtpException;
import cn.hutool.extra.ftp.FtpMode;
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
@@ -18,6 +19,15 @@ import java.io.ByteArrayOutputStream;
*/
public class FtpFileClient extends AbstractFileClient {
+ /**
+ * 连接超时时间,单位:毫秒
+ */
+ private static final Long CONNECTION_TIMEOUT = 3000L;
+ /**
+ * 读写超时时间,单位:毫秒
+ */
+ private static final Long SO_TIMEOUT = 10000L;
+
private Ftp ftp;
public FtpFileClient(Long id, FtpFileClientConfig config) {
@@ -26,9 +36,12 @@ 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()));
+ // 初始化 Ftp 对象:https://gitee.com/zhijiantianya/yudao-cloud/pulls/207/
+ FtpConfig ftpConfig = new FtpConfig(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(),
+ CharsetUtil.CHARSET_UTF_8, null, null);
+ ftpConfig.setConnectionTimeout(CONNECTION_TIMEOUT);
+ ftpConfig.setSoTimeout(SO_TIMEOUT);
+ this.ftp = new Ftp(ftpConfig, FtpMode.valueOf(config.getMode()));
}
@Override
@@ -72,4 +85,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..788325f6c 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,9 +1,14 @@
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.core.util.StrUtil;
+import cn.hutool.extra.ftp.FtpConfig;
+import cn.hutool.extra.ssh.JschRuntimeException;
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;
+import com.jcraft.jsch.JSch;
import java.io.File;
@@ -14,6 +19,20 @@ import java.io.File;
*/
public class SftpFileClient extends AbstractFileClient {
+ /**
+ * 连接超时时间,单位:毫秒
+ */
+ private static final Long CONNECTION_TIMEOUT = 3000L;
+ /**
+ * 读写超时时间,单位:毫秒
+ */
+ private static final Long SO_TIMEOUT = 10000L;
+
+ 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) {
@@ -22,18 +41,27 @@ public class SftpFileClient extends AbstractFileClient {
@Override
protected void doInit() {
- // 初始化 Ftp 对象
- this.sftp = new Sftp(config.getHost(), config.getPort(), config.getUsername(), config.getPassword());
+ // 初始化 Sftp 对象
+ FtpConfig ftpConfig = new FtpConfig(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(),
+ CharsetUtil.CHARSET_UTF_8, null, null);
+ ftpConfig.setConnectionTimeout(CONNECTION_TIMEOUT);
+ ftpConfig.setSoTimeout(SO_TIMEOUT);
+ 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);
}