java - 为什么关闭 JSCH Channel 后,SFTP 连接仍然存在?

标签 java sftp jsch

当下面的代码完成运行时,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/

相关文章:

php - 使用 phpseclib nlist 从后缀/扩展名过滤的 SFTP 文件夹中获取文件

java - org.apache.commons.vfs2.FileSystemException : Could not connect to SFTP server

java - 如何正确转换存储库 @Query 的结果?

java - 找到问题点的更好方法

java - 组合treeMaps以输出相似的值

linux - 使用 JSch 执行命令时没有得到任何输出

java - JSch ChannelSftp 退出状态始终为 -1

java - 如何调用带参数的私有(private)方法

azure - ##[错误]未处理的: handle is not a Buffer CopyFilesOverSSH in Azure DevOps build pipeline

python - 写入使用 Paramiko/pysftp "open"方法打开的 SFTP 服务器上的文件很慢