1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Valter Silva
  • 16,446
  • 52
  • 137
  • 218

1 Answers1

0

Use the .resolveFile overload that takes the baseFile parameter:

public FileObject resolveFile(FileObject baseFile, String uri,
                              FileSystemOptions fileSystemOptions)

This overload is not in the FileSystemManager interface. But it's in the default implementation, the DefaultFileSystemManager. So you have to cast the fsManager to the DefaultFileSystemManager.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I couldn't find this method that you refer. https://commons.apache.org/proper/commons-vfs/apidocs/org/apache/commons/vfs2/FileSystemManager.html – Valter Silva Jul 13 '16 at 12:44