當下面的代碼運行完畢時,netstat -a|grep sftp
顯示一個打開的SFTP連接。它也顯示爲JProfiler中的一個開放連接。爲什麼JSCH通道關閉後SFTP連接仍然存在?
channel.isConnected()
在最後版畫假。任何想法爲什麼連接不會因爲我處於虧損而關閉?
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;
}
人們可以有一個會話內多種渠道(無論是在同一時間,以及順序),因此它是一件好事,本次會議是一個通道關閉時不會自動關閉。 –
這是不正確的 - 退出只是斷開連接。沒有其他的。 –
sftpChannel.exit()通過ChannelSftp繼承的Channel類(Channel.java的494行)調用disconnect方法,通過自己的disconnect方法(Session.java的1545行)調用session.disconnect。 –