I'm trying to put a file in a remote FTP host. This is how I usually do:
String ftpUri =
"ftp://" + target.get(server).get("Username") + ":{" + target.get(server).get("Psswd") + "}@";
if (StringUtils.isBlank(target.get(server).get("Port"))) {
ftpUri += target.get(server).get("Hostname") + target.get(server).get("RemotePath");
} else {
ftpUri += target.get(server).get("Hostname") + ":" + target.get(server).get("Port") + target.get(server)
.get("RemotePath");
}
System.out.println("ftpUri = " + ftpUri);
FileSystemManager fsManager;
try {
fsManager = VFS.getManager();
} catch (FileSystemException e) {
throw new RuntimeException("Failed to get fsManager from VFS", e);
}
FileSystemOptions opts = new FileSystemOptions();
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
FileObject remoteFile = fsManager.resolveFile(ftpUri, opts); // <- it breaks
My ftUri
is:
ftp://[username]:{[hash_password]}@[hostname]:21/users/afolder/anotherfolder/afile.pdf
But I'm getting the following exception:
Caused by: org.apache.commons.vfs2.FileSystemException: Could not change to work directory "/".
at org.apache.commons.vfs2.provider.ftp.FtpClientFactory.createConnection(FtpClientFactory.java:130)
... 19 more
Is it possible to set a working directory path?
My guess is that the FTP library is trying to work on the root
directory, which I don't have any permission; however, I'm informing another path via the URI, but somehow the library insists to work in the root
directory.