1

I have to generate and put some files on WinSCP using java program. For connecting to box (with WinSCP), I use following credentials: host name: username: port: private key file:

I searched through How to retrieve a file from a server via SFTP? and found below code:

import com.jcraft.jsch.*;
public class TestJSch 
{
    public static void main(String args[]) {
        JSch jsch = new JSch();
        Session session = null;`enter code here`
        try {
            session = jsch.getSession("username", "127.0.0.1", 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("password");
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;
            sftpChannel.get("remotefile.txt", "localfile.txt");
            sftpChannel.exit();
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace();  
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }
}

All these codes are using password whereas I don't have password but a private key file.Can anybody help me to know how can I connect to server using a private key file only?

Community
  • 1
  • 1

0 Answers0