Java SFTP (apache vfs2) - 密码@ [英] Java SFTP (apache vfs2) - password with @
本文介绍了Java SFTP (apache vfs2) - 密码@的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 org.apache.commons.vfs2 通过 SFTP 下载文件.问题是,密码包含@"字符,因此这会导致 URI 解析不正确:
I'm trying to use the org.apache.commons.vfs2 to download a file via SFTP. The problem is, the password contains the '@' char, so this causes the URI to be parsed incorrectly:
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);
推荐答案
使用 实际的 URI 构造器 而不是手动滚动您自己的:
Use an actual URI constructor instead of hand-rolling your own:
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);
这篇关于Java SFTP (apache vfs2) - 密码@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文