传输文件时 JSch SFTP 代码挂起

JSch SFTP code hangs when tranferring a file

我的任务是将一些文件从服务器复制到本地,我搜索了很多有关连接库的信息并找到了 JSch。我使用了下面的代码,但是读取或移动文件花费了太多时间。不知道有没有用

JSch jsch = new JSch();
Session session = null;
try {
    // set up session
    session = jsch.getSession("userName","hostIP");
    // use private key instead of username/password
    session.setConfig(
            "PreferredAuthentications",
            "publickey,gssapi-with-mic,keyboard-interactive,password");
    jsch.addIdentity("***.ppk");
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();

    // copy remote log file to localhost.
    ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
    channelSftp.connect();
    channelSftp.setInputStream(System.in);
    channelSftp.setOutputStream(System.out);
    System.out.println("shell channel connected....");

    ChannelSftp c = (ChannelSftp) channelSftp;

    System.out.println("done");
    channelSftp.get("report.xml", "C:\Users\akrishnan");
    channelSftp.exit();

} catch (Exception e) {
    e.printStackTrace();
} finally {
    session.disconnect();
}

是否有任何库可用于使用私钥文件 (**.ppk) 从我的 Java 代码连接服务器?

这很有可能,导致挂起的原因:

channelSftp.setInputStream(System.in);
channelSftp.setOutputStream(System.out);

对 "sftp" 频道这样做会破坏一切。这没有道理。只需删除这两行。

查看官方JSch SFTP example – 没有这样的调用

有关使用 JSch 进行文件传输的正确代码,请参阅: SFTP file transfer using Java JSch.


强制警告:不要使用StrictHostKeyChecking=no盲目接受所有主机密钥。这是一个安全漏洞。您失去了针对 MITM attacks.

的保护

有关正确(和安全)的方法,请参阅: