简体   繁体   English

Java SFTP(Apache VFS2)-密码@

[英]Java SFTP (apache vfs2) - password with @

I'm trying to use the org.apache.commons.vfs2 to download a file via SFTP. 我正在尝试使用org.apache.commons.vfs2通过SFTP下载文件。 The problem is, the password contains the '@' char, so this causes the URI to be parsed incorrectly: 问题是,密码包含'@'字符,因此这会导致URI被错误地解析:

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

Does anyone has an idea how to get around this issue? 有谁知道如何解决这个问题? (I can't change the password, obviously). (显然,我无法更改密码)。 This is the code I'm using: 这是我正在使用的代码:

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);

Use an actual URI constructor instead of hand-rolling your own: 使用实际的URI构造函数,而不要手动滚动自己的URI构造函数

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM