Home > Java > javaTutorial > How Can I Retrieve Files from an SFTP Server Using Java?

How Can I Retrieve Files from an SFTP Server Using Java?

DDD
Release: 2024-12-03 17:48:10
Original
219 people have browsed it

How Can I Retrieve Files from an SFTP Server Using Java?

Retrieving Files via SFTP in Java

Secure File Transfer Protocol (SFTP) is a secure file transfer protocol that provides secure file transfers over an SSH connection. This is in contrast to other protocols like FTPS which utilizes TLS/SSL over a standard TCP connection.

To retrieve a file from a server via SFTP using Java, you can utilize the JSch library. JSch is widely used by projects like Eclipse, Ant, and Apache Commons HttpClient due to its robust features and user-friendly interface.

It supports user/pass and certificate-based logins and offers plenty of SSH2 features. Here's a basic code example that you can use to retrieve a remote file over SFTP:

import com.jcraft.jsch.*;

public class SftpFileRetriever {

    public static void main(String[] args) throws JSchException {
        JSch jsch = new JSch();
        String knownHostsFilename = "/home/username/.ssh/known_hosts";
        jsch.setKnownHosts(knownHostsFilename);

        Session session = jsch.getSession("remote-username", "remote-host");
        {
            // Interactive version
            // This version allows you to selectively update specified known_hosts file.
            // You'll need to implement the UserInfo interface, and MyUserInfo is a swing implementation provided in JSch's Sftp.java example.
            UserInfo ui = new MyUserInfo();
            session.setUserInfo(ui);

            // Non-interactive version
            // This version relies on the host key being in the known-hosts file.
            // session.setPassword("remote-password");
        }

        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();

        ChannelSftp sftpChannel = (ChannelSftp) channel;

        sftpChannel.get("remote-file", "local-file");

        sftpChannel.exit();
        session.disconnect();
    }
}
Copy after login

This code establishes an SSH session, sets up the known hosts, and connects to the channel. Then, it retrieves the remote file and disconnects the session. Error handling and specific details of user interactions are left as an exercise for you to complete.

The above is the detailed content of How Can I Retrieve Files from an SFTP Server Using Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template