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://[email protected]/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
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:
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:
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
as an explicit call instead, everything works fine...!