简体   繁体   中英

Secure FTP using private key authentication

I am using JSCH library to do a sftp between two servers. But, I am unable to load the file into the URL using getResource() , for which I get an error during runtime. I checked the permissions and they are 600, so we should be able to read it.

    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 file = new File("/home/dev85adm/.ssh/id_rsa");
if (file.exists()) {
    URL keyFileURL = file.toURL();
    //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM