使用HTTP URL时,JSch无法使用addIdentity查找私钥

时间:2019-07-05 16:50:20

标签: java ssh jsch private-key

我一直在使用JSch与OpenSSH服务器建立SFTP连接,并且在此过程中,当我尝试以URI形式添加私钥作为标识时,私钥未被识别。

当我尝试从浏览器运行URL时,它运行正常。

我尝试了所有使用转义字符的方法,但是没有用。

Session session = null;
ChannelSftp channelSftp = null;

log.info(file.getOriginalFilename());
log.info(privatekey);
JSch jsch = new JSch();
jsch.addIdentity(this.privatekey);
log.info("identity added ");
session = jsch.getSession(username,hostname,portno);
log.info("Session Created");
session.setPassword(password);

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(60000);
session.connect();
log.info("session connected.....");
channelSftp = (ChannelSftp) session.openChannel("sftp");
log.info("Channel Opened.....");
log.info(channelSftp.toString());

channelSftp.setInputStream(System.in);
channelSftp.setOutputStream(System.out);
channelSftp.connect();
log.info("Channel connected.....");
//channelSftp.cd(destination);
log.info(file.getOriginalFilename());
channelSftp.put(file.getInputStream(), file.getOriginalFilename(), ChannelSftp.OVERWRITE);
channelSftp.put(file.getInputStream(),file.getOriginalFilename());


channelSftp.disconnect();
session.disconnect();

我希望必须使用添加身份来添加密钥,但不幸的是,我收到以下错误消息

  

[https-jsse-nio-10443-exec-10] cdalrsBlkCounterpartyServiceImpl:遇到的错误是:com.jcraft.jsch.JSchException:java.io.FileNotFoundException:http:/staging.fileserver.com/properties/xyz/ xyz / xyz.ppk(没有此类文件或目录)。

文件在此位置http://staging.fileserver.com/properties/xyz/xyz/xyz.ppk

中可用

1 个答案:

答案 0 :(得分:0)

JSch.addIdentity method的文档说:

  

prvkey-私钥文件的文件名。 ...

“文件名” = 本地文件名的路径,而不是 HTTP URL

如果您要从 HTTP 服务器下载文件(从安全角度考虑,这是一个糟糕的主意),则必须对此进行编码。