【问题标题】:Best way in Java to write a file direct to a remote system via scp and sftp [closed]Java中通过scp和sftp将文件直接写入远程系统的最佳方法[关闭]
【发布时间】:2016-03-06 12:49:16
【问题描述】:

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

谷歌搜索为此提供了多种选择。 Jsch 似乎是最热门的。对这里的最佳方法有何看法?

我更愿意避免使用开源软件包,除非它们成熟且有据可查(我曾在一些开源产品上遇到过糟糕的经历,这些产品可能使复杂的工作变得简单,但也让简单的工作变得痛苦)。

【问题讨论】:

  • 意见有什么问题?有没有更好的论坛可以让我问这样的问题?

标签: java sftp scp jsch


【解决方案1】:

我建议使用 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();
        }
    }
}

【讨论】:

  • 谢谢,我先看看这个
【解决方案2】:

我不会建议使用哪个库,但您需要的是库为您提供一个 OutputStream,您可以将数据直接写入其中。我相信大多数库都提供了这个 API。

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多