diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ece8a530..ec52f5568 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.26(2024-01-22) +# 5.8.26(2024-01-24) ### 🐣新特性 * 【db 】 RedisDS增加user支持(issue#I8XEQ4@Gitee) @@ -11,6 +11,7 @@ * 【crypto】 修复BouncyCastleProvider导致graalvm应用报错UnsupportedFeatureError(pr#3464@Github) * 【http 】 修复UserAgentUtil对QQ浏览器识别问题(issue#I8X5XQ@Gitee) * 【core 】 修复BeanToMapCopier获取类型数组越界问题(issue#3468@Github) +* 【extra 】 修复SshjSftpSession关闭导致的问题(issue#3472@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.25(2024-01-11) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java index f593eb121..6bbc8b621 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java @@ -19,6 +19,9 @@ import java.util.List; */ public abstract class AbstractFtp implements Closeable { + /** + * 默认编码 + */ public static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8; protected FtpConfig ftpConfig; diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java index 7e59d9077..617d11da3 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java @@ -35,6 +35,7 @@ public class SshjSftp extends AbstractFtp { private SSHClient ssh; private SFTPClient sftp; + private Session session; /** * 构造,使用默认端口 @@ -42,7 +43,7 @@ public class SshjSftp extends AbstractFtp { * @param sshHost 主机 */ public SshjSftp(String sshHost) { - this(new FtpConfig(sshHost, 22, null, null, CharsetUtil.CHARSET_UTF_8)); + this(new FtpConfig(sshHost, 22, null, null, DEFAULT_CHARSET)); } /** @@ -211,12 +212,9 @@ public class SshjSftp extends AbstractFtp { @Override public void close() { - try { - sftp.close(); - ssh.disconnect(); - } catch (IOException e) { - throw new FtpException(e); - } + IoUtil.close(this.session); + IoUtil.close(this.sftp); + IoUtil.close(this.ssh); } /** @@ -246,16 +244,36 @@ public class SshjSftp extends AbstractFtp { * @since 5.7.19 */ public String command(String exec) { - Session session = null; + final Session session = this.initSession(); + + Session.Command command = null; try { - session = ssh.startSession(); - final Session.Command command = session.exec(exec); + command = session.exec(exec); InputStream inputStream = command.getInputStream(); - return IoUtil.read(inputStream, DEFAULT_CHARSET); + return IoUtil.read(inputStream, this.ftpConfig.getCharset()); } catch (Exception e) { throw new FtpException(e); } finally { - IoUtil.close(session); + IoUtil.close(command); } } + + /** + * 初始化Session并返回 + * + * @return session + */ + private Session initSession() { + Session session = this.session; + if (null == session || !session.isOpen()) { + IoUtil.close(session); + try { + session = this.ssh.startSession(); + } catch (final Exception e) { + throw new FtpException(e); + } + this.session = session; + } + return session; + } }