java - 将目录上传到远程服务器

标签 java jsch

我想做的是用java将一个目录从本地服务器上传到远程服务器。

为此,我使用了 com.jcraft.jsch 库。

我可以毫无问题地连接到远程服务器并将目录从本地计算机上传到远程服务器。

我创建了这个方法来做到这一点:

private void recursiveFolderUpload(String sourcePath, String destinationPath, ChannelSftp channelSftp)
        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(),
                        channelSftp);
            }

        }
    }

这没有问题,目录已转移,但有时我的目录中有快捷方式。

假设我有一个文件夹。其中我还有两个文件夹,其中一个是另一个文件夹的快捷方式。

执行该方法时,快捷方式的目录现在是它自己的文件,我不希望这样。

将目录上传到远程服务器时如何保留快捷方式?

谢谢

最佳答案

好吧,你必须编码。

检测本地文件实际上是符号链接(symbolic link): Detecting a symlink in Java .

并使用 ChannelSftp.symlink 在服务器上重新创建符号链接(symbolic link).

关于java - 将目录上传到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063195/

相关文章:

java - 使用 JSch 连接到远程服务器时获取 UnknownHostKey

java - 使适配器变量时出错 GetData()

java - 用一种类型参数化的方法接受两种类型

java - 带有 jsch 的 scp 文件给出 'unexpected filename'

java - JSch 给出 Java 生成的 key 对无效的私钥错误

ant - com.jcraft.jsch.JSchException : Auth cancel

java - DAO 层内的 JDBC 连接

java - ClassNotFoundException : org. apache.log4j.Priority 使用 Filenet API 时

java - 将对象添加到 ArrayList 时遇到问题

java - 当 key 加密时如何使用 Java 和 JSch 连接到远程 SSH 机器