java - Java中通过scp和sftp直接将文件写入远程系统的最佳方法

标签 java sftp scp jsch

因此,在我的 Java 客户端中,我将创建一个新文件,我希望通过 scp 或 sftp(用户可以选择哪个)将其直接写入远程系统 - 我不希望将文件写入本地文件系统然后复制过来(生成的文件可能很大,本地磁盘空间可能会出现问题)。

谷歌搜索为此提供了多种选择。 Jsch 似乎是最受欢迎的。对这里的最佳方法有什么看法吗?

我更喜欢避免使用开源软件包,除非它们成熟且有据可查(我在使用几种开源产品时有过不好的经历,这些产品可能使复杂的工作变得简单,但也使简单的工作成为一种痛苦)。

最佳答案

我建议使用 JSch,因为它工作可靠且易于使用。看一下我不久前为可行性测试而制作的这个示例。您会看到数据来自 FileInputStream。您可以轻松更改此设置以直接发送字节,无需中间文件。

请注意,此示例忽略 SSL 证书:

/**
 * Uploads a local file to a remote host.
 */
public class Copy {

    /** Session to run commands */
    private Session session;

    /**
     * Creates a session to the remote host with the provided username and password data. Ignores certificates.
     * @param host remote host
     * @param user login name
     * @param pass password
     * @throws JSchException
     */
    public Copy(String host, String user, String pass) throws JSchException {
        this.session = createSession(host, user, pass);
    }

    /**
     * Creates a session from the provided connection data. The certificate is ignored when creating the session!
     * @param host remote host
     * @param user login name
     * @param pass password
     * @return SSH session
     * @throws JSchException
     */
    private Session createSession(String host, String user, String pass) throws JSchException {
        // Ignore certificate
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");

        // Create session
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, 22);
        session.setConfig(config);
        session.setPassword(pass);
        return session;
    }

    /**
     * Copies the local file to the remote path.
     * @param srcPath path to local file
     * @param dstPath target path
     * @throws JSchException
     * @throws IOException
     * @throws SftpException
     */
    public void cp(Path srcPath, String dstPath) throws JSchException, IOException, SftpException {
        // This basically comes from JSch examples
        session.connect();
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        // Assume the target is a path and the target file name will be the source file name
        String targetPath = dstPath;
        String targetFile = srcPath.getFileName().toString();
        try {
            channel.cd(dstPath);
        } catch (SftpException e) {
            // Target does not exist
            int lastIndexOf = targetPath.lastIndexOf('/');
            // target can also be only a file name
            if (lastIndexOf > -1) {
                targetFile = targetPath.substring(lastIndexOf + 1);
                targetPath = targetPath.substring(0, lastIndexOf + 1);
                channel.cd(targetPath);
            }
        }
        try {
            channel.put(new FileInputStream(srcPath.toFile()), targetFile, ChannelSftp.OVERWRITE);
        } finally {
            channel.exit();
            session.disconnect();
        }
    }
}

关于java - Java中通过scp和sftp直接将文件写入远程系统的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34040029/

相关文章:

linux - SCP 一个文件的第一行到另一个系统

java - SFTP Mule 客户端 Java API - 用户登录超时问题

java - SFTP非空文件夹递归删除

linux - scp 从 Linux 到 Windows

java - 将带空格的字符串从 shell 传递到 java

python-2.7 - 在python中使用sftp从远程路径获取文件到本地目录

linux - 如何将文件从 Vagrant 机器复制到 localhost

java - 无法解析属性 - hibernate

java - 获取 PhantomJS 返回给 Selenium 驱动程序的状态码

java - 检查 null 的最佳方法?