Java如何读取SFTP上的文件

在Java中,我们可以使用JSch库来实现从SFTP(SSH File Transfer Protocol)服务器上读取文件。JSch是一个纯Java实现的SSH2协议,可以用于连接和操作SFTP服务器。

引入JSch库

首先,我们需要在项目中引入JSch库。可以通过Maven来添加依赖项,将以下代码添加到pom.xml文件中:

<dependencies>
  <dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
  </dependency>
</dependencies>

或者,你也可以直接下载JSch库的JAR文件并将其添加到项目中。

建立SFTP连接

在读取SFTP上的文件之前,我们需要建立与SFTP服务器的连接。下面是建立SFTP连接的代码示例:

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SftpExample {
    public static void main(String[] args) {
        String host = "your_sftp_host";
        int port = 22;
        String username = "your_username";
        String password = "your_password";

        try {
            JSch jSch = new JSch();
            Session session = jSch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // 在这里进行文件读取操作

            channelSftp.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上面的代码中,我们使用JSch库创建一个JSch实例,并使用指定的主机、端口、用户名和密码创建一个Session对象。然后,我们设置StrictHostKeyChecking属性为"no",以便禁用主机密钥检查。接下来,我们使用Session对象打开一个sftp通道,并连接到SFTP服务器。

读取文件

在建立了与SFTP服务器的连接后,我们可以使用ChannelSftp对象来读取文件。下面是一个示例,演示如何从SFTP服务器上读取文件并将其保存到本地文件系统中:

import com.jcraft.jsch.*;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class SftpExample {
    public static void main(String[] args) {
        String host = "your_sftp_host";
        int port = 22;
        String username = "your_username";
        String password = "your_password";
        String remoteFilePath = "/path/to/remote/file.txt";
        String localFilePath = "/path/to/local/file.txt";

        try {
            JSch jSch = new JSch();
            Session session = jSch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            InputStream inputStream = channelSftp.get(remoteFilePath);
            OutputStream outputStream = new FileOutputStream(localFilePath);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            channelSftp.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上面的代码中,我们使用ChannelSftp对象的get()方法来获取远程文件的输入流。然后,我们使用Java的IO类将输入流中的数据写入本地文件中。

完整代码

下面是一个完整的示例代码,演示了如何建立SFTP连接并读取文件:

import com.jcraft.jsch.*;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class SftpExample {
    public static void main(String[] args) {
        String host = "your_sftp_host";
        int port = 22;
        String username = "your_username";
        String password = "your_password";
        String remoteFilePath = "/path/to/remote/file.txt";
        String localFilePath = "/path/to/local/file.txt";

        try {
            JSch jSch = new JSch();
            Session session = jSch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp