修复SshjSftpSession关闭导致的问题

This commit is contained in:
Looly 2024-01-24 10:09:27 +08:00
parent 4df7a25cc4
commit 22081e879a
3 changed files with 35 additions and 13 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.26(2024-01-22) # 5.8.26(2024-01-24)
### 🐣新特性 ### 🐣新特性
* 【db 】 RedisDS增加user支持issue#I8XEQ4@Gitee * 【db 】 RedisDS增加user支持issue#I8XEQ4@Gitee
@ -11,6 +11,7 @@
* 【crypto】 修复BouncyCastleProvider导致graalvm应用报错UnsupportedFeatureErrorpr#3464@Github * 【crypto】 修复BouncyCastleProvider导致graalvm应用报错UnsupportedFeatureErrorpr#3464@Github
* 【http 】 修复UserAgentUtil对QQ浏览器识别问题issue#I8X5XQ@Gitee * 【http 】 修复UserAgentUtil对QQ浏览器识别问题issue#I8X5XQ@Gitee
* 【core 】 修复BeanToMapCopier获取类型数组越界问题issue#3468@Github * 【core 】 修复BeanToMapCopier获取类型数组越界问题issue#3468@Github
* 【extra 】 修复SshjSftpSession关闭导致的问题issue#3472@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.25(2024-01-11) # 5.8.25(2024-01-11)

View File

@ -19,6 +19,9 @@ import java.util.List;
*/ */
public abstract class AbstractFtp implements Closeable { public abstract class AbstractFtp implements Closeable {
/**
* 默认编码
*/
public static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8; public static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8;
protected FtpConfig ftpConfig; protected FtpConfig ftpConfig;

View File

@ -35,6 +35,7 @@ public class SshjSftp extends AbstractFtp {
private SSHClient ssh; private SSHClient ssh;
private SFTPClient sftp; private SFTPClient sftp;
private Session session;
/** /**
* 构造使用默认端口 * 构造使用默认端口
@ -42,7 +43,7 @@ public class SshjSftp extends AbstractFtp {
* @param sshHost 主机 * @param sshHost 主机
*/ */
public SshjSftp(String 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 @Override
public void close() { public void close() {
try { IoUtil.close(this.session);
sftp.close(); IoUtil.close(this.sftp);
ssh.disconnect(); IoUtil.close(this.ssh);
} catch (IOException e) {
throw new FtpException(e);
}
} }
/** /**
@ -246,16 +244,36 @@ public class SshjSftp extends AbstractFtp {
* @since 5.7.19 * @since 5.7.19
*/ */
public String command(String exec) { public String command(String exec) {
Session session = null; final Session session = this.initSession();
Session.Command command = null;
try { try {
session = ssh.startSession(); command = session.exec(exec);
final Session.Command command = session.exec(exec);
InputStream inputStream = command.getInputStream(); InputStream inputStream = command.getInputStream();
return IoUtil.read(inputStream, DEFAULT_CHARSET); return IoUtil.read(inputStream, this.ftpConfig.getCharset());
} catch (Exception e) { } catch (Exception e) {
throw new FtpException(e); throw new FtpException(e);
} finally { } 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;
}
} }