Java SFTP(apache vfs2) - 密码@

时间:2014-10-19 13:15:34

标签: java sftp apache-commons-vfs

我尝试使用org.apache.commons.vfs2通过SFTP下载文件。 问题是,密码包含' @' char,所以这会导致URI被错误地解析:

org.apache.commons.vfs2.FileSystemException: Expecting / to follow the hostname in URI

有没有人知道如何解决这个问题? (显然,我无法更改密码)。这是我使用的代码:

String sftpUri = "sftp://" + userName + ":" + password + "@"
        + remoteServerAddress + "/" + remoteDirectory + fileName;

String filepath = localDirectory + fileName;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());

FileObject remoteFile = manager.resolveFile(sftpUri, opts);
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);

1 个答案:

答案 0 :(得分:2)

使用an actual URI constructor而不是自己动手:

String userInfo = userName + ":" + password;
String path = remoteDirectory + filename;  // Need a '/' between them?
URI sftpUri = new URI("sftp", userInfo, remoteServerAddress, -1, path, null, null);
...
FileObject remoteFile = manager.resolveFile(sftpUri.toString(), opts);