首页 > 解决方案 > 使用 JSCH 将文件从一个远程服务器发送到另一个使用 JSCH 的服务器

问题描述

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

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

标签: javasftpjsch

解决方案


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

推荐阅读