我正在尝试将文件放入远程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
目录中工作。
使用采用 baseFile
参数的.resolveFile
重载:
public FileObject resolveFile(FileObject baseFile, String uri,
FileSystemOptions fileSystemOptions)
此重载不在FileSystemManager
接口中。但它在默认实现中,DefaultFileSystemManager
.所以你必须把fsManager
投到DefaultFileSystemManager
.