From d11693fb2de0d22c4e4156d10e3c0bfd0ffd654f Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 24 Nov 2024 15:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DSftp=E4=B8=AD=E4=BC=A0?= =?UTF-8?q?=E5=85=A5Session=E9=87=8D=E8=BF=9E=E6=97=B6=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98(issue#IB69U8@Gitee)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../main/java/cn/hutool/extra/ssh/Sftp.java | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a1aa83a3..ddf320244 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * 【cron 】 修复cron模块依赖log模块问题 * 【extra 】 修复MailUtil发送html格式邮件无法正常展示图片问题(pr#1279@Gitee) * 【core 】 【可能的向下兼容问题】修复双引号转义符转义错误问题,修改规则后,对非闭合双引号字段的策略变更,如"aa,则被识别为aa(issue#IB5UQ8@Gitee) +* 【extra 】 修复Sftp中传入Session重连时逻辑错误问题(issue#IB69U8@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.33(2024-11-05) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java index bbca18dd0..865f216d4 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java @@ -8,13 +8,9 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.extra.ftp.AbstractFtp; import cn.hutool.extra.ftp.FtpConfig; import cn.hutool.extra.ftp.FtpException; -import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.*; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.ChannelSftp.LsEntrySelector; -import com.jcraft.jsch.Session; -import com.jcraft.jsch.SftpATTRS; -import com.jcraft.jsch.SftpException; -import com.jcraft.jsch.SftpProgressMonitor; import java.io.File; import java.io.InputStream; @@ -111,7 +107,7 @@ public class Sftp extends AbstractFtp { * @since 4.1.14 */ public Sftp(Session session, Charset charset) { - super(FtpConfig.create().setCharset(charset)); + super(FtpConfig.create().setCharset(charset).setHost(session.getHost()).setPort(session.getPort())); init(session, charset); } @@ -172,6 +168,17 @@ public class Sftp extends AbstractFtp { * @since 5.3.3 */ public void init() { + // issue#IB69U8 如果用户传入Session对象,则不能使用配置初始化,而是尝试重新连接 + if(StrUtil.isEmpty(this.ftpConfig.getHost()) && null != this.session){ + try { + this.session.connect((int) this.ftpConfig.getConnectionTimeout()); + } catch (JSchException e) { + throw new JschRuntimeException(e); + } + init(this.session, this.ftpConfig.getCharset()); + return; + } + init(this.ftpConfig); }