【问题标题】:Apache VFS error. because it is a relative path, and no base URI was providedApache VFS 错误。因为它是相对路径,并且没有提供基本 URI
【发布时间】:2021-10-29 09:36:44
【问题描述】:

我正在使用 Spring Boot 构建服务。我需要列出来自 SFTP 的文件并下载匹配的文件。

我正在使用 org.apache.commons,commons-vfs2 - 2.4 版

以前,我的代码可以正常工作。它可以直接遍历下载我想要的文件。但它目前会引发错误。

错误是

Could not find file with URI "sftp://myusername@xx.xx.xxx.174/output" because it is a relative path, and no base URI was provided.

我不明白发生了什么,因为它曾经有效。

这是我发生错误的代码行,

FileSystemManager manager = VFS.getManager();
FileObject fileObject = manager.resolveFile("sftp://" + sftp_username + "@" + sftp_host+"/output"); //error this line
FileObject[] files = fileObject.getChildren();
//... for loop the files

【问题讨论】:

    标签: java spring maven apache-commons-vfs


    【解决方案1】:

    可能是一个答案...

    我遇到了完全相同的问题 - 只是第一次对我有用,但在以后的尝试中却没有!

    我有一个我经常调用的方法,它将文件的副本上传到 SFTP 服务器上的不同文件夹:

      private static void copyToSftpLocations(...)
      {
        [...]
              try (FileSystemManager manager = VFS.getManager())
              {
                FileSystemOptions opts = new FileSystemOptions();
                SftpFileSystemConfigBuilder.getInstance().setSessionTimeout(opts, java.time.Duration.ofMillis(sftpSessionTimeout));
                SftpFileSystemConfigBuilder.getInstance().setConnectTimeout(opts, java.time.Duration.ofMillis(sftpSessionTimeout));
                SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, "publickey,keyboard-interactive,password");
                SftpFileSystemConfigBuilder.getInstance().setKnownHosts(opts, new File("known_hosts.txt"));
                LOG.debug("Attempting to connect to  "+sftpServer+" as "+sftpUserName+"...");
                try (FileObject remoteServer = manager.resolveFile("sftp://"+URLEncoder.encode(sftpUserName,StandardCharsets.UTF_8.toString())+":"+URLEncoder.encode(sftpPassword,StandardCharsets.UTF_8.toString())+"@"+sftpServer,opts))
                {
                  while (...)
                  {
                    String sftpDirName = ...;
                    String remotePath = "/"+sftpDirName;
                    try (FileObject remoteFolder = remoteServer.resolveFile(remotePath))
                    {
                      if (!remoteFolder.exists())
                      {
                        remoteFolder.createFolder();
                      }
                      try (FileObject local = manager.resolveFile(theMasterPath.toUri());
                           FileObject remoteFile = remoteFolder.resolveFile(theMasterPath.getFileName().toString()))
                      {
                        remoteFile.copyFrom(local, Selectors.SELECT_SELF);
                      }
      
    

    我第一次调用该方法时,一切正常 - 每个文件夹中的文件副本。 我第二次及以后调用该方法时,我得到了:

    org.apache.commons.vfs2.FileSystemException: Could not find file with URI "sftp://<user>:***@<address>" because it is a relative path, and no base URI was provided.
    

    我在创建 FileObject remoteServer 时尝试使用 sftpServer+"/",但没有任何区别。

    我根本不明白为什么最初有效的东西没有继续有效。此外,基本上相同的代码在我的项目中的其他地方使用,并且每次都有效!

    我正在使用 commons-vfs2 2.9.0 版。

    edit:我刚刚发现,如果我删除第一个 try-resource 并放入

    FileSystemManager manager = VFS.getManager();
    

    改为显式调用,一切正常...!

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2018-01-22
      • 2023-01-10
      • 1970-01-01
      相关资源
      最近更新 更多