sftp add refactoring method

This commit is contained in:
bwcx_jzy 2022-06-12 21:20:47 +08:00
parent 77818aa376
commit 3ab1c45228
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
2 changed files with 56 additions and 2 deletions

View File

@ -114,9 +114,24 @@ public class JschUtil {
* @return SSH会话 * @return SSH会话
*/ */
public static Session openSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) { public static Session openSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
return openSession(sshHost, sshPort, sshUser, privateKeyPath, passphrase, 0);
}
/**
* 打开一个新的SSH会话
*
* @param sshHost 主机
* @param sshPort 端口
* @param sshUser 用户名
* @param privateKeyPath 私钥的路径
* @param passphrase 私钥文件的密码可以为null
* @param timeOut 超时时间单位毫秒
* @return SSH会话
*/
public static Session openSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase, int timeOut) {
final Session session = createSession(sshHost, sshPort, sshUser, privateKeyPath, passphrase); final Session session = createSession(sshHost, sshPort, sshUser, privateKeyPath, passphrase);
try { try {
session.connect(); session.connect(timeOut);
} catch (JSchException e) { } catch (JSchException e) {
throw new JschRuntimeException(e); throw new JschRuntimeException(e);
} }

View File

@ -77,8 +77,21 @@ public class Sftp extends AbstractFtp {
* @since 5.3.3 * @since 5.3.3
*/ */
public Sftp(FtpConfig config) { public Sftp(FtpConfig config) {
this(config, false);
}
/**
* 构造
*
* @param config FTP配置
* @param init 是否立即初始化
* @since 5.8.4
*/
public Sftp(FtpConfig config, boolean init) {
super(config); super(config);
init(config); if (init) {
init(config);
}
} }
/** /**
@ -102,6 +115,32 @@ public class Sftp extends AbstractFtp {
init(session, charset); init(session, charset);
} }
/**
* 构造
*
* @param session {@link Session}
* @param charset 编码
* @param timeOut 超时时间单位毫秒
* @since 5.8.4
*/
public Sftp(Session session, Charset charset, long timeOut) {
super(FtpConfig.create().setCharset(charset).setConnectionTimeout(timeOut));
init(session, charset);
}
/**
* 构造
*
* @param channel {@link ChannelSftp}
* @param charset 编码
* @param timeOut 超时时间单位毫秒
* @since 5.8.4
*/
public Sftp(ChannelSftp channel, Charset charset, long timeOut) {
super(FtpConfig.create().setCharset(charset).setConnectionTimeout(timeOut));
init(channel, charset);
}
/** /**
* 构造 * 构造
* *