|
|
|
@ -1,6 +1,8 @@
|
|
|
|
package cn.iocoder.yudao.module.infra.framework.file.core.client.sftp;
|
|
|
|
package cn.iocoder.yudao.module.infra.framework.file.core.client.sftp;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
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.hutool.extra.ssh.Sftp;
|
|
|
|
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
|
|
|
|
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
|
|
|
|
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
|
|
|
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
|
|
|
@ -14,6 +16,11 @@ import java.io.File;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> {
|
|
|
|
public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
|
|
|
// 某些旧的sftp服务器仅支持ssh-dss协议,该协议并不安全,默认不支持该协议,按需添加
|
|
|
|
|
|
|
|
JSch.setConfig("server_host_key", JSch.getConfig("server_host_key") + ",ssh-dss");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Sftp sftp;
|
|
|
|
private Sftp sftp;
|
|
|
|
|
|
|
|
|
|
|
|
public SftpFileClient(Long id, SftpFileClientConfig config) {
|
|
|
|
public SftpFileClient(Long id, SftpFileClientConfig config) {
|
|
|
|
@ -23,17 +30,26 @@ public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void doInit() {
|
|
|
|
protected void doInit() {
|
|
|
|
// 初始化 Ftp 对象
|
|
|
|
// 初始化 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
|
|
|
|
@Override
|
|
|
|
public String upload(byte[] content, String path, String type) {
|
|
|
|
public String upload(byte[] content, String path, String type) {
|
|
|
|
// 执行写入
|
|
|
|
// 执行写入
|
|
|
|
String filePath = getFilePath(path);
|
|
|
|
String filePath = getFilePath(path);
|
|
|
|
|
|
|
|
String fileName = FileUtil.getName(filePath);
|
|
|
|
|
|
|
|
String dir = StrUtil.removeSuffix(filePath, fileName);
|
|
|
|
File file = FileUtils.createTempFile(content);
|
|
|
|
File file = FileUtils.createTempFile(content);
|
|
|
|
reconnectIfTimeout();
|
|
|
|
reconnectIfTimeout();
|
|
|
|
sftp.mkDirs(FileUtil.getParent(filePath, 1)); // 需要创建父目录,不然会报错
|
|
|
|
sftp.mkDirs(dir); // 需要创建父目录,不然会报错
|
|
|
|
sftp.upload(filePath, file);
|
|
|
|
boolean success = sftp.upload(filePath, file);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
|
|
|
throw new JschRuntimeException(StrUtil.format("上传文件到目标目录 ({}) 失败", filePath));
|
|
|
|
|
|
|
|
}
|
|
|
|
// 拼接返回路径
|
|
|
|
// 拼接返回路径
|
|
|
|
return super.formatFileUrl(config.getDomain(), path);
|
|
|
|
return super.formatFileUrl(config.getDomain(), path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|