我正在尝试使用 org.apache.commons.vfs2 通过 SFTP 下载文件。
问题是,密码包含“@”字符,因此这会导致 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);
Use 一个实际的 URI 构造函数 http://docs.oracle.com/javase/7/docs/api/java/net/URI.html#URI(java.lang.String,%20java.lang.String,%20java.lang.String,%20int,%20java.lang.String,%20java.lang.String,%20java.lang.String)而不是自己动手:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)