Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

I'm trying to retrieve a file from a server using SFTP (as opposed to FTPS) using Java. How can I do this?

1 Answer

0 votes
by (46k points)

Here is the complete source code of an example using JSch without having to worry about the ssh key checking.

import com.jcraft.jsch.*;

public class TestJSch {

    public static void main(String args[]) {

        JSch jsch = new JSch();

        Session session = null;

        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();

        }

    }

}

Related questions

0 votes
1 answer
asked Aug 4, 2020 in AWS by Amyra (12.9k points)
0 votes
1 answer
asked Aug 4, 2020 in AWS by Amyra (12.9k points)
0 votes
1 answer
asked Jul 31, 2019 in AWS by yuvraj (19.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...