[英]Secure FTP using private key authentication
I am using JSCH library to do a sftp between two servers. 我正在使用JSCH库在两个服务器之间执行sftp。 But, I am unable to load the file into the URL
using getResource()
, for which I get an error during runtime. 但是,我无法使用getResource()
将文件加载到URL
,因此在运行时会出错。 I checked the permissions and they are 600, so we should be able to read it. 我检查了权限,它们是600,所以我们应该可以阅读它。
JSch jsch = new JSch();
File file = new File("/home/xxxxxx/.ssh/id_rsa");
Session session = null;
URL keyFileURL = null;
URI keyFileURI = null;
if (file.exists())
{
keyFileURL = SecureFTP.class.getClassLoader().getResource("/home/xxxxxx/.ssh/id_rsa");
if (keyFileURL == null)
{
System.out.println("what");
throw new RuntimeException("Key file not found in classpath");
}
}
try{
keyFileURI = keyFileURL.toURI();
}
catch(Exception URISyntaxException)
{
System.out.println("Wrong URL. LOL");
}
jsch.addIdentity(new File(keyFileURI).getAbsolutePath());
Is there something I am missing, in loading the resource or the path? 在加载资源或路径时我缺少什么吗?
Why don't you use the File.toURL
为什么不使用File.toURL
File file = new File("/home/dev85adm/.ssh/id_rsa");
if (file.exists()) {
URL keyFileURL = file.toURL();
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.