mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复SshjSftpSession关闭导致的问题
This commit is contained in:
parent
b7508cc0e9
commit
dd1ec90fd4
@ -89,6 +89,7 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
|
|
||||||
private SSHClient ssh;
|
private SSHClient ssh;
|
||||||
private SFTPClient sftp;
|
private SFTPClient sftp;
|
||||||
|
private Session session;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
@ -120,7 +121,9 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
* @since 5.7.18
|
* @since 5.7.18
|
||||||
*/
|
*/
|
||||||
public void init() {
|
public void init() {
|
||||||
this.ssh = SshjUtil.openClient(this.ftpConfig.getConnector());
|
if(null == this.ssh){
|
||||||
|
this.ssh = SshjUtil.openClient(this.ftpConfig.getConnector());
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ssh.setRemoteCharset(ftpConfig.getCharset());
|
ssh.setRemoteCharset(ftpConfig.getCharset());
|
||||||
@ -248,12 +251,9 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
IoUtil.closeQuietly(this.session);
|
||||||
sftp.close();
|
IoUtil.closeQuietly(this.sftp);
|
||||||
ssh.disconnect();
|
IoUtil.closeQuietly(this.ssh);
|
||||||
} catch (final IOException e) {
|
|
||||||
throw new FtpException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -283,16 +283,36 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
* @since 5.7.19
|
* @since 5.7.19
|
||||||
*/
|
*/
|
||||||
public String command(final String exec) {
|
public String command(final 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);
|
|
||||||
final InputStream inputStream = command.getInputStream();
|
final InputStream inputStream = command.getInputStream();
|
||||||
return IoUtil.read(inputStream, DEFAULT_CHARSET);
|
return IoUtil.read(inputStream, this.ftpConfig.getCharset());
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
} finally {
|
} finally {
|
||||||
IoUtil.closeQuietly(session);
|
IoUtil.closeQuietly(command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化Session并返回
|
||||||
|
*
|
||||||
|
* @return session
|
||||||
|
*/
|
||||||
|
private Session initSession() {
|
||||||
|
Session session = this.session;
|
||||||
|
if (null == session || !session.isOpen()) {
|
||||||
|
IoUtil.closeQuietly(session);
|
||||||
|
try {
|
||||||
|
session = this.ssh.startSession();
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new FtpException(e);
|
||||||
|
}
|
||||||
|
this.session = session;
|
||||||
|
}
|
||||||
|
return session;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user