I'm trying to copy a single file in the local file system to a remote SFTP server using Apache Commons VFS. to mimic the actual problem, I have written the following code which generates the same issue.
FileSystemManager fileSystemManager = VFS.getManager();
FileObject fileToCopy = fileSystemManager.resolveFile("/tmp/submission/2004220.csv");
FileObject destinationDirectory = fileSystemManager.resolveFile("/tmp/test");
destinationDirectory.copyFrom(fileToCopy,Selectors.SELECT_SELF);
When the above code executed, the /tmp/test
directory is converted to a file instead of copying the 2004220.csv
file into /tmp/test
folder. I was able to successfully copy files between two folders by selecting all files as children through a Selectors.SELECT_CHILDREN
Fileselector, but facing this problem only when copying a single file to a directory.
Finally, I was able to find a workaround. I'm not sure whether the above issue is due to an issue in VFS or that is not the way which VFS expects to handle individual file copy between folders. Anyway here is my solution.
if you want to keep the full file path in the FileObject then,
FileSystemManager fileSystemManager = VFS.getManager();
FileObject fileToCopy = fileSystemManager.resolveFile("/tmp/submission/2004220.csv");
FileObject destinationDirectory = fileSystemManager.resolveFile("/tmp/test");
NameFileFilter nameFileFilter = new NameFileFilter(Arrays.asList(fileToCopy.getName().getBaseName()));
FileSelector fileSelector = new FileFilterSelector(nameFileFilter);
destinationDirectory.copyFrom(fileToCopy.getParent(),fileSelector);
or else,
FileSystemManager fileSystemManager = VFS.getManager();
FileObject fileToCopy = fileSystemManager.resolveFile("/tmp/submission");
FileObject destinationDirectory = fileSystemManager.resolveFile("/tmp/test");
NameFileFilter nameFileFilter = new NameFileFilter(Arrays.asList("2004220.csv"));
FileSelector fileSelector = new FileFilterSelector(nameFileFilter);
destinationDirectory.copyFrom(fileToCopy,fileSelector);