当前位置: 首页 > 知识库问答 >
问题:

JSch-服务器“home”路径始终为“/”,具有freeshd,并且无法更改目录

廖永长
2023-03-14

我用freeshd设置了一个服务器,并能够将其放置,包括更改目录和列出文件。我有一些例子。txt文件和主目录中的文件夹。我使用freeshd将服务器上的主目录设置为“C:\SFTP”(而不是定义目录为“$home\”的主变量)。

显然,在使用JSch时,

        JSch jsch = new JSch();
        session = jsch.getSession(username,host,port);

        jsch.addIdentity(key.getAbsolutePath());

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");

        session.setConfig(config);
        session.setUserInfo(new MyUserInfo());
        session.connect();

        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp)channel;

        System.out.println("Home: "+channelSftp.getHome());

最后一行只打印“主页:/”。任何尝试(在上述代码之后立即进行)使用

channelSftp.cd(WORKINGDIR);

结果在

2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2185)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1295)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1267)
at test.SFTPTest.main(SFTPTest.java:71)

我想如果我找到了为什么JSch没有正确的主路径(或任何路径)的根源这会有用的。而且,奇怪的是,我上传和下载文件时使用put()和get()没有问题。

我听说过各种各样的事情,人们查看源代码,发现它在解析路径时做了一些奇怪的事情,并且使用了一个名为“_realPath()”的方法和多余的前导/尾随“/”,但我甚至没有它告诉我连接后主目录是正确的。

有什么想法吗?

共有2个答案

申高卓
2023-03-14

SFTP目录应该是当前用户的目录,不一定是SFTP服务用的目录,我也遇到了同样的问题,因为我用的目录和用户的默认目录设置。

西门振
2023-03-14

谢谢大家的评论。

在windows XP操作系统中,我安装了freeshd并设置了默认目录,然后,当我尝试通过控制台ssh连接时,目录是“/”,我正在编写chdir,但目录是:C:\windows\system32\n这很混乱。。。

我的Java代码:

public void recursiveFolderUpload(String sourcePath, String destinationPath) throws FileNotFoundException {



        if (c == null || session == null || !session.isConnected() || !c.isConnected()) {
            log.debug("Connection to server is closed. Open it first.");
        }

        try {

            // c.put(sourceFile, destinationFile);
            // log.info("Upload successfull.");
            File sourceFile = new File(sourcePath);
            if (sourceFile.isFile()) {
                // copy if it is a file
                c.cd(destinationPath);
                if (!sourceFile.getName().endsWith("."))
                    c.put(new FileInputStream(sourceFile), sourceFile.getName(), c.OVERWRITE);
            } else {
                log.info("Inside else " + sourceFile.getName());
                File[] files = sourceFile.listFiles();

                if (files != null && !sourceFile.getName().startsWith(".")) {
                    log.info("Directory remote server: " + c.pwd());
                    c.cd(destinationPath);
                    SftpATTRS attrs = null;

                    // check if the directory is already existing
                    try {
                        attrs = c.stat(destinationPath + sourceFile.getName());
                    } catch (Exception e) {
                        log.warn(destinationPath + sourceFile.getName() + " not found");
                        //e.printStackTrace();
                    }

                    // else create a directory
                    if (attrs != null) {
                        log.info("Directory exists IsDir : " + attrs.isDir());
                    } else {
                        log.info("Creating dir /" + sourceFile.getName());
                        c.mkdir(sourceFile.getName());
                    }

                    for (File f : files) {

                        if(!f.getName().contains(".dtd")){
                            log.info("Uploading file: " + f.getAbsoluteFile());
                            recursiveFolderUpload(f.getAbsolutePath(), destinationPath + sourceFile.getName() + "/");
                        }           
                    }
                }

            }
        } catch (SftpException e) {
            e.printStackTrace();
        }

    }

我的解决方案只是在名为recursiveFolderUpload的方法的输入参数destinationPath中加上“/”

换句话说,我的属性文件并非如此:

properties.host             = IP
properties.user             = user
properties.pass             = pass
properties.port             = port
properties.dir              = /    ---> This points to the directory configured by default in opensshd within windows 

再次感谢一切。

 类似资料:
  • 我正试图弄清楚如何使用Jsch SFTP转到父目录。 我将当前路径存储在一个字符串中,我猜是channelsftp.cd(string);才是我该用的。但是在当前路径上使用substring感觉不对。有没有更好的办法做到这一点?

  • 我正在使用Swagger为Spring boot2微服务创建API留档,我是Swagger的新手。我的Spring启动应用程序具有上下文路径 /api/user/因此该应用程序可在localhost:8080/api/user访问。swagger默认用户界面可在/swagger-ui.html访问。所以从我的项目来看,网址是localhost:8080/api/user/swagger-ui.ht

  • 我刚刚安装了weblogic server和OSB的新副本。在快速启动屏幕上成功安装weblogic 10.3.6后,我尝试配置域。但是,屏幕没有进一步处理,下面的屏幕截图中出现了错误。 此外,在eclipse中,当我尝试添加服务器时,它会提示我创建一个域,但这也不起作用。我在控制台中遇到的错误是:

  • 我在我的项目上有一个这样的目录,当我运行以下命令时,我在其中创建文件到目录: 上面的错误命令是什么?以及如何为此将创建到目录中?

  • 问题内容: 我使用安装程序安装了MySQL服务器,然后启动了该服务器。重新启动后,我尝试再次启动它并得到错误: 我试图重新安装MySQL。 更新: 当我以管理员身份运行时,什么都没有发生。 问题答案: 由于您使用Windows安装程序,因此一切都已设置为可将MySQL 5.7作为Windows服务运行,这在大多数情况下是一个不错的选择。 而不是从命令行运行, 跑 右键点击 启动服务。

  • 我正在使用RHEL8,它安装了默认的OpenJDK。哪个java命令指向/usr/bin/java。java-version提供openjdk版本“1.8.0_252” 已在/u01/app/java/location中安装java。修改了。如下图所示 现在JAVA_HOME指向/u01/app/JAVA/jdk1。8.0_241/但哪种java或java版本仍然指向OpenJDK。 bash_p