【问题标题】:Send files from one remote server using JSCH to another server using JSCH too使用 JSCH 将文件从一个远程服务器发送到另一个使用 JSCH 的服务器
【发布时间】:2023-06-15 21:47:01
【问题描述】:

我想将文件从我的第一台远程服务器发送到另一台:

public boolean uploadFile() throws JSchException, SftpException {
        ChannelSftp channelSftpA = createChannelSftp();
        ChannelSftp channelSftpB = createChannelSftp();
        channelSftpA.connect();
        channelSftpB.connect();

        localFilePath = "/data/upload/readme.txt";
        remoteFilePath = "/bingo/pdf/";

        channelSftpA.cd(localFilePath);
        channelSftpA.put(localFilePath + "readme.txt", remoteFilePath + "readme.txt");

但它不起作用。我应该将channelB.put 放入我的第一个channelA.put 吗?

【问题讨论】:

  • 但它不起作用。 - 请提供更多详细信息。顺便说一句,当调用channelSftpA.put() 时,您将readme.txt 附加到localFilePath,其中已经包含readme.txt

标签: java sftp jsch


【解决方案1】:

如果我对您的问题的理解正确,您的代码将从第三台服务器运行,为了传输文件,您应该从server A 获取文件,然后放在server B 上。顺便说一下,您要下载和上传文件的用户应该有权访问指定的文件夹!

private boolean transferFile() throws JSchException, SftpException {
        ChannelSftp channelSftpA = createChannelSftp();
        ChannelSftp channelSftpB = createChannelSftp();
        channelSftpA.connect();
        channelSftpB.connect();

        String fileName = "readme.txt";
        String remoteFilePathFrom = "/folderFrom/";
        String remoteFilePathTo = "/folderTo/";

        InputStream srcInputStream = channelSftpA.get(remoteFilePathFrom + fileName);
        channelSftpB.put(srcInputStream, remoteFilePathTo + fileName);
        System.out.println("Transfer has been completed");

        channelSftpA.exit();
        channelSftpB.exit();
        return true;
    }

【讨论】:

  • 很高兴能帮上忙!