【发布时间】:2015-04-10 09:43:34
【问题描述】:
目前我正在使用 apache vfs2 从 sftp 下载文件。对于身份验证,我使用用户名和密码。
有没有办法只使用公私钥而不使用密码来使用 vfs2?
我想我已经使用了这个功能,但是如何使用呢?只设置为“是”?
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no");
这是我当前的代码(sn-p):
private boolean downloadFile(){
StandardFileSystemManager sysManager = new StandardFileSystemManager();
//download der Datei
try {
sysManager.init();
FileObject localFile = sysManager.resolveFile(localFilePath);
FileObject remoteFile = sysManager.resolveFile(createConnectionString(host, user, password, fileName, port),createDefaultOptions());
//Selectors.SELECT_FILES --> A FileSelector that selects only the base file/folder.
localFile.copyFrom(remoteFile, Selectors.SELECT_FILES);
} catch (Exception e) {
logger.error("Downloading file failed: " + e.toString());
return false;
}finally{
sysManager.close();
}
return true;
}
和
private FileSystemOptions createDefaultOptions() throws FileSystemException{
//create options for sftp
FileSystemOptions options = new FileSystemOptions();
//ssh key
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no");
//set root directory to user home
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, true);
//timeout
SftpFileSystemConfigBuilder.getInstance().setTimeout(options, timeout);
return options;
}
【问题讨论】:
-
嘿,我们只需要在文件中提供私钥还是同时提供两个密钥??
标签: java sftp public-key-encryption apache-commons-vfs