Aşağıdaki kod çalıştırmayı bitirdiğinde, netstat -a|grep sftp
açık bir SFTP bağlantısı gösterir. Aynı zamanda JProfiler'de açık bir bağlantı olarak ortaya çıkıyor.yanlış nihayet blok baskılarJSCH Kanalı kapatıldıktan sonra neden bir SFTP bağlantısı var?
channel.isConnected()
. Bağlantıların neden kayıp olduğumdan kapanmadığı hakkında bir fikrin var mı?
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()
aşağıda ekleme:
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;
}
Bir tek oturumda içinde birden fazla kanal olabilir (her ikisi de aynı anda hem de ardışık devam), bu nedenle oturumu olduğunu iyi bir şeydir Bir kanal kapatıldığında otomatik kapanmaz. –
Bu doğru değil - çıkış sadece bağlantıyı keser. Başka hiçbir şey. –
sftpChannel.exit(), ChannelSftp'in (Channel.java'nın 494 satırı) hangi kanal sınıfını kullandığını kesmek yöntemini kullanır, session.disconnect kendi bağlantı kesme yöntemiyle (Session.java satır 1545) çağrılır. –