使用java中的SFTP将目录复制到远程计算机

时间:2014-01-28 15:28:49

标签: java sftp

我需要通过SFTP将目录从本地计算机复制到远程计算机。我已经完成了通过JSCH API复制文件,但它不适用于目录。有什么建议吗?

我正在使用以下代码:

    JSch jsch = new JSch();
    String filename = localFile.getName();
    com.jcraft.jsch.Session sftpsession = jsch.getSession(username, hostname, 22);
    sftpsession.setUserInfo(new HardcodedUserInfo(password));
    Properties config = new Properties();
    config.setProperty("StrictHostKeyChecking", "no");
    sftpsession.setConfig(config);
    sftpsession.connect();
    ChannelSftp channel = (ChannelSftp)sftpsession.openChannel("sftp");
    channel.connect();
    channel.cd(remoteDirectory);
    channel.put(new FileInputStream(localFile), filename);
    channel.disconnect();
    sftpsession.disconnect();

3 个答案:

答案 0 :(得分:1)

JSCH没有一个函数可以通过SFTP递归发送或接收目录。您的代码必须构建要在远程系统上创建的文件和目录列表,然后调用ChannelSftp.mkdir()ChannelSftp.put()来创建目录和文件。

还要记住,在创建子目录之前需要创建父目录。例如,如果目录mkdir("/foo/bar/baz")不存在,/foo/bar将失败。

答案 1 :(得分:0)

您可以使用JSCH java API在远程服务器上递归复制文件夹。

下面是相同的示例代码示例 -

private static void recursiveFolderUpload(String sourcePath, String destinationPath) throws SftpException, FileNotFoundException {

    File sourceFile = new File(sourcePath);
    if (sourceFile.isFile()) {

        // copy if it is a file
        channelSftp.cd(destinationPath);
        if (!sourceFile.getName().startsWith("."))
            channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);

    } else {

        System.out.println("inside else " + sourceFile.getName());
        File[] files = sourceFile.listFiles();

        if (files != null && !sourceFile.getName().startsWith(".")) {

            channelSftp.cd(destinationPath);
            SftpATTRS attrs = null;

            // check if the directory is already existing
            try {
                attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName());
            } catch (Exception e) {
                System.out.println(destinationPath + "/" + sourceFile.getName() + " not found");
            }

            // else create a directory
            if (attrs != null) {
                System.out.println("Directory exists IsDir=" + attrs.isDir());
            } else {
                System.out.println("Creating dir " + sourceFile.getName());
                channelSftp.mkdir(sourceFile.getName());
            }

            for (File f: files) {
                recursiveFolderUpload(f.getAbsolutePath(), destinationPath + "/" + sourceFile.getName());
            }

        }
    }

}

您可以参考链接here了解有关此代码的详细信息。

答案 2 :(得分:0)

您可以使用此代码复制目录:

copy(File localFile, String destPath, ChannelSftp clientChannel)
{
    if (localFile.isDirectory()) {
        clientChannel.mkdir(localFile.getName());
        System.out.println("Created Folder: " + localFile.getName() + " in " + destPath);

        destPath = destPath + "/" + localFile.getName();
        clientChannel.cd(destPath);

        for (File file : localFile.listFiles()) {
            copy(file, destPath, clientChannel);
        }

        clientChannel.cd(destPath.substring(0, destPath.lastIndexOf('/')));
    } else {
        System.out.println("Copying File: " + localFile.getName() + " to " + destPath);
        clientChannel.put(new FileInputStream(localFile), localFile.getName(), ChannelSftp.OVERWRITE);
    }
}
相关问题