Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用SFTP将目录复制到远程计算机_Java_Sftp - Fatal编程技术网

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

在java中使用SFTP将目录复制到远程计算机,java,sftp,Java,Sftp,我需要通过SFTP将目录从本地计算机复制到远程计算机。我已经通过jschapi复制了一个文件,但它在目录上不起作用。有什么建议吗 我正在使用以下代码: JSch jsch = new JSch(); String filename = localFile.getName(); com.jcraft.jsch.Session sftpsession = jsch.getSession(username, hostname, 22); sftpsession.setU

我需要通过
SFTP
将目录从本地计算机复制到远程计算机。我已经通过
jschapi
复制了一个文件,但它在目录上不起作用。有什么建议吗

我正在使用以下代码:

    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”)
将失败。

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

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

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

我不知道JSCHAPI应该如何工作,但您可以做的一件事是检查文件是否是目录,在这种情况下,将所有文件放入其中并逐个发送。