使用 JSCH 将文件从一台远程服务器发送到另一台也使用 JSCH 的服务器

Send files from one remote server using JSCH to another server using JSCH too

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

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 吗?

如果我理解你的问题是正确的,你的代码将是来自第三个服务器的 运行,为了传输文件你应该从 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;
    }