1

我正在尝试将文件放在远程 FTP 主机中。这是我通常的做法:

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

ftUri的是:

ftp://[username]:{[hash_password]}@[hostname]:21/users/afolder/anotherfolder/afile.pdf

但我得到以下异常:

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

是否可以设置工作目录路径?

我的猜测是 FTP 库正在尝试在root我没有任何权限的目录上工作;但是,我通过 URI 通知了另一个路径,但是库以某种方式坚持在root目录中工作。

4

1 回答 1

0

使用带参数的.resolveFile重载baseFile

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

此重载不在FileSystemManager接口中。但它在默认实现中,DefaultFileSystemManager. 因此,您必须fsManagerDefaultFileSystemManager.

于 2016-07-13T10:58:43.763 回答