当下面的代码完成运行时,netstat -a|grep sftp
显示一个打开的 SFTP 连接。它还在 JProfiler 中显示为打开的连接。
channel.isConnected()
在 finally block 中打印 false。有什么想法为什么连接没有关闭,因为我不知所措吗?
public static void clean() {
com.jcraft.jsch.ChannelSftp channel = null;
try {
channel = Helper.openNewTLSftpChannel();
channel.connect();
channel.cd(remoteFileDirectory);
List<ChannelSftp.LsEntry> list = channel.ls("*." + fileType);
for (ChannelSftp.LsEntry file : list) {
String fileName = file.getFilename();
DateTime fileDate = new DateTime(parseDateFromFileName(fileName));
//if this file is older than the cutoff date, delete from the SFTP share
if (fileDate.compareTo(cleanupCutoffdate) < 0) {
channel.rm(fileName);
}
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
System.out.println(channel.isConnected());
}
}
}
在下面添加openNewTLSftpChannel()
:
public static ChannelSftp openNewSftpChannel(String privateKeyFileName, String password, String username, String host, int port)
throws ConfigurationErrorException {
JSch jsch = new JSch();
File sftpPrivateFile = new File(privateKeyFileName);
Channel channel;
try {
if (!sftpPrivateFile.canRead()) {
throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath());
}
jsch.addIdentity(sftpPrivateFile.getAbsolutePath(), password);
Session session = jsch.getSession(username, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
} catch (JSchException jschException) {
throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath());
}
return (ChannelSftp) channel;
}
最佳答案
如果您查看 JSCH examples对于 SFTP,您将看到 session 是如何终止的:
//setup Session here
...
session.connect();
...
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
...run sftp logic...
//close sessions here
sftpChannel.exit();
session.disconnect();
您会注意到连接和断开有两个部分; Session 对象和 Channel 对象。
在我的代码中,我使用 Session 对象来设置我的身份验证信息,并使用 Channel 对象来执行我需要的 sftp 命令。
在您的实例中,您正在 openNewSftpChannel 方法中创建 Session 对象,但它永远不会关闭,因此您的 session 保持 Activity 状态。
有关更多上下文,请查看示例。
关于java - 为什么关闭 JSCH Channel 后,SFTP 连接仍然存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15768516/