4

I'm building a service using Spring boot. I need to list files from SFTP and download the matched file.

I'm using org.apache.commons, commons-vfs2 - version 2.4

Previously, my code worked normally. It can iterate through the directly to download my wanted file. But it currently throws error.

The error is

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.

I do not understand what happened because it used to work.

Here is my line of the code that occurs the error,

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
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Hikaru Shindo
  • 2,611
  • 8
  • 34
  • 59

3 Answers3

0

This may be an answer...

I was getting exactly the same issue - only with me it worked the first time, but not on subsequent attempts!

I have a method which I call every so often, which uploads copies of a file to different folders on the SFTP server:

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

The first time I called the method, everything worked fine - a copy of the file in each folder. The second & subsequent times I called the method, I got:

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.

I tried using sftpServer+"/" when creating the FileObject remoteServer, but it made no difference.

I simply could not see why something that worked first didn't continue to work. Furthermore, fundamentally the same code is used elsewhere in my project, and it works every time!

I'm using commons-vfs2 version 2.9.0.

edit: I have just discovered that if I remove the first try-resource and put

FileSystemManager manager = VFS.getManager();

as an explicit call instead, everything works fine...!

davejbur
  • 245
  • 1
  • 2
  • 7
0

This comment solved my issue :

This is normally caused by missing dependencies. You need at least JSch, commons-logging and commons-Lang on the classpath.

https://issues.apache.org/jira/browse/VFS-773

MuhammetK
  • 115
  • 1
  • 7
0

For me, the problem was that the commons-vfs2 has as an optional dependency the JSch library, which means that when maven/sbt resolves the needed dependencies for commons-vfs2, it doesn't automatically download the Jsch library.

So, I fixed the issue by adding the jsch library explicitly in build.sbt/pom.xml

Cosmin
  • 676
  • 7
  • 15