当前位置: 首页 > 工具软件 > JSch > 使用案例 >

java sftp连接池_在JSCH中使用连接池

罗鸿畴
2023-12-01

我正在使用JSCH进行sftp文件上传。在当前状态下,每个线程都会在需要时打开和关闭连接。

是否可以在JSCH中使用连接池,以避免由于大量打开和关闭连接而导致的开销?

这是从线程内部调用的函数的示例

public static void file_upload(String filename) throws IOException {

JSch jsch = new JSch();

Session session = null;

try {

session = jsch.getSession("user", "server_name", 22);

session.setConfig("StrictHostKeyChecking", "no");

session.setPassword("super_secre_password");

session.connect();

Channel channel = session.openChannel("sftp");

channel.connect();

ChannelSftp sftpChannel = (ChannelSftp) channel;

FileInputStream inputSrr = new FileInputStream(filename);

try {

sftpChannel.put(inputSrr, "/var/temp/"+filename);

} catch (SftpException e) {

e.printStackTrace();

} finally {

if (inputSrr != null) {

inputSrr.close();

}

}

sftpChannel.exit();

session.disconnect();

} catch (JSchException e) {

e.printStackTrace();

} catch (SftpException e) {

e.printStackTrace();

}

}

 类似资料: