繁体   English   中英

在Java中使用SFTP将目录复制到远程计算机

[英]Copy directory to a remote machine using SFTP in java

我需要通过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();

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

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

您可以使用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());
            }

        }
    }

}

您可以在此处参考链接以获取有关此代码的更多详细信息。

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

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM