|
|
@@ -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<SftpFileClientConfig> {
|
|
|
|
|
|
+ /**
|
|
|
+ * 连接超时时间,单位:毫秒
|
|
|
+ */
|
|
|
+ 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<SftpFileClientConfig> {
|
|
|
|
|
|
@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);
|
|
|
}
|