Java中通过sftp协议实现上传下载的示例代码

 更新时间:2024年06月24日 09:22:51   作者:ldcaws  
在java开发中遇到需要将linux系统中指定目录下的文件下载到windows本地的需求,本文就来介绍Java中通过sftp协议实现上传下载,具有一定的参考价值,感兴趣的可以了解一下

在java开发中,遇到需要将linux系统中指定目录下的文件下载到windows本地的需求,下面聊聊通过sftp协议实现上传和下载。

1、SFTP协议

JSch是Java Secure Channel的缩写。JSch是一个SSH2的纯Java实现。它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到你自己的应用程序。
SFTP是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。SFTP 为 SSH的一部份,是一种传输文件到服务器的安全方式。SFTP是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

2、SFTP核心类

ChannelSftp类是JSch实现SFTP核心类,它包含了所有SFTP的方法,如:

  • put(): 文件上传
  • get(): 文件下载
  • cd(): 进入指定目录
  • ls(): 得到指定目录下的文件列表
  • rename(): 重命名指定文件或目录
  • rm(): 删除指定文件
  • mkdir(): 创建目录
  • rmdir(): 删除目录
    其他参加源码。
    JSch支持三种文件传输模式:
  • OVERWRITE 完全覆盖模式,这是JSch的默认文件传输模式,即如果目标文件已经存在,传输的文件将完全覆盖目标文件,产生新的文件。
  • RESUME 恢复模式,如果文件已经传输一部分,这时由于网络或其他任何原因导致文件传输中断,如果下一次传输相同的文件,则会从上一次中断的地方续传。
  • APPEND 追加模式,如果目标文件已存在,传输的文件将在目标文件后追加。

编写一个工具类,根据ip,用户名及密码得到一个SFTP channel对象,即ChannelSftp的实例对象,在应用程序中就可以使用该对象来调用SFTP的各种操作方法:

public class SFTPChannel {
    Session session = null;
    Channel channel = null;

    private static final Logger LOG = Logger.getLogger(SFTPChannel.class.getName());

    public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {

        String ftpHost = sftpDetails.get(“host”);
        String port = sftpDetails.get("port");
        String ftpUserName = sftpDetails.get("username");
        String ftpPassword = sftpDetails.get("password");

        int ftpPort = 22;
        if (port != null && !port.equals("")) {
            ftpPort = Integer.valueOf(port);
        }

        JSch jsch = new JSch(); // 创建JSch对象
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象
        LOG.debug("Session created.");
        if (ftpPassword != null) {
            session.setPassword(ftpPassword); // 设置密码
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立链接
        LOG.debug("Session connected.");

        LOG.debug("Opening Channel.");
        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName
                + ", returning: " + channel);
        return (ChannelSftp) channel;
    }

    public void closeChannel() throws Exception {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}

3、实例

import com.jcraft.jsch.*;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class SFTPDownloadWithRateLimit {


    public static void main(String[] args) {
        String host = "hostname";
        String username = "username";
        String password = "password";
        String remoteFile = "/path/to/remote/file";
        String localFile = "localFile";
        int rateLimit = 1024; // 1KB/s


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


            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            
            // 设置下载速率限制
            channelSftp.setInputStream(new BufferedInputStream(channelSftp.get(remoteFile), rateLimit));
            
            InputStream inputStream = channelSftp.get(remoteFile);
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));


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


            inputStream.close();
            outputStream.close();
            channelSftp.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException | java.io.IOException e) {
            e.printStackTrace();
        }
    }
}

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

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

public class SFTPDownloadWithRateLimit {

    public static void main(String[] args) {
        String host = "sftp.example.com";
        String username = "username";
        String password = "password";
        int port = 22;
        String remoteFilePath = "/path/to/remote/file";
        String localFilePath = "/path/to/local/file";
        int downloadRate = 1024; // 1 KB/s

        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);
            FileOutputStream outputStream = new FileOutputStream(localFilePath);

            byte[] buffer = new byte[1024];
            int bytesRead;
            long startTime = System.currentTimeMillis();

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
                long elapsedTime = System.currentTimeMillis() - startTime;
                long expectedTime = (outputStream.getChannel().size() / downloadRate) * 1000;
                if (elapsedTime < expectedTime) {
                    Thread.sleep(expectedTime - elapsedTime);
                }
            }

            inputStream.close();
            outputStream.close();
            channelSftp.disconnect();
            session.disconnect();

            System.out.println("File downloaded successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上面是根据指定速率进行下载。

4、工具类

import com.jcraft.jsch.*;
import java.io.*;
public class SFTPUtil {

    private String host;
    private int port;
    private String username;
    private String password;

    public SFTPUtil(String host, int port, String username, String password) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public void uploadFile(String localFilePath, String remoteFilePath) {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

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

            channelSftp.put(new FileInputStream(localFilePath), remoteFilePath);

            channelSftp.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void downloadFile(String remoteFilePath, String localFilePath) {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

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

            channelSftp.get(remoteFilePath, new FileOutputStream(localFilePath));

            channelSftp.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}

JSch支持在文件传输时对传输进度的监控。可以实现JSch提供的SftpProgressMonitor接口来完成这个功能。

  • init(): 当文件开始传输时,调用init方法。
  • count(): 当每次传输了一个数据块后,调用count方法,count方法的参数为这一次传输的数据块大小。
  • end(): 当传输结束时,调用end方法。
public class MyProgressMonitor implements SftpProgressMonitor {
    private long transfered;

    @Override
    public boolean count(long count) {
        transfered = transfered + count;
        System.out.println("Currently transferred total size: " + transfered + " bytes");
        return true;
    }

    @Override
    public void end() {
        System.out.println("Transferring done.");
    }

    @Override
    public void init(int op, String src, String dest, long max) {
        System.out.println("Transferring begin.");
    }
}

到此这篇关于Java中通过sftp协议实现上传下载的示例代码的文章就介绍到这了,更多相关Java sftp上传下载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Java使用反射获取字段属性

    Java使用反射获取字段属性

    这篇文章主要为大家详细介绍了Java如何利用反射实现获取字段属性值,文中的示例代码讲解详细,具有很好的参考价值,希望对大家有所帮助
    2023-06-06
  • 在IDEA中安装MyBatis Log Plugin插件,执行mybatis的sql语句(推荐)

    在IDEA中安装MyBatis Log Plugin插件,执行mybatis的sql语句(推荐)

    这篇文章主要介绍了在IDEA中安装MyBatis Log Plugin插件,执行mybatis的sql语句,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • springboot如何使用thymeleaf模板访问html页面

    springboot如何使用thymeleaf模板访问html页面

    springboot中推荐使用thymeleaf模板,使用html作为页面展示。那么如何通过Controller来访问来访问html页面呢?下面通过本文给大家详细介绍,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-05-05
  • Java虚拟机JVM优化实战的过程全记录

    Java虚拟机JVM优化实战的过程全记录

    有人说Java之所以能够崛起,JVM功不可没。Java虚拟机最初服务于让Java语言凌驾于平台之上,实现“编写一次,到处运行”,那么下面这篇文章主要给大家分享了个关于Java虚拟机JVM优化实战的过程全记录,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • Mybatis映射文件详解之mapper.xml文件

    Mybatis映射文件详解之mapper.xml文件

    本文详细介绍了Mybatis映射文件的结构、标签和使用方法,包括mapper元素、标签的主要属性如id、parameterType、resultType以及动态SQL和结果映射的使用,通过映射文件,可以将数据库表与Java对象映射,支持查询、插入、更新、删除等操作,提高了SQL语句的灵活性和可重用性
    2024-09-09
  • 解决spring.thymeleaf.cache=false不起作用的问题

    解决spring.thymeleaf.cache=false不起作用的问题

    这篇文章主要介绍了解决spring.thymeleaf.cache=false不起作用的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • SpringBoot(cloud)自动装配bean找不到类型的问题

    SpringBoot(cloud)自动装配bean找不到类型的问题

    这篇文章主要介绍了SpringBoot(cloud)自动装配bean找不到类型的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java中的ArrayList(扩容机制)详解

    Java中的ArrayList(扩容机制)详解

    ArrayList作为Java中广泛使用的动态数组,其扩容机制是保证性能和内存使用平衡的关键,默认初始容量为10,扩容因子为1.5,旨在减少频繁的内存分配和数据迁移代价,同时建议使用预估计的初始化容量以减少扩容次数
    2024-11-11
  • mybatis 自定义实现拦截器插件Interceptor示例

    mybatis 自定义实现拦截器插件Interceptor示例

    这篇文章主要介绍了mybatis 自定义实现拦截器插件Interceptor,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 通过实例讲解springboot整合WebSocket

    通过实例讲解springboot整合WebSocket

    这篇文章主要介绍了通过实例讲解springboot整合WebSocket,WebSocket为游览器和服务器提供了双工异步通信的功能,即游览器可以向服务器发送消息,服务器也可以向游览器发送消息。,需要的朋友可以参考下
    2019-06-06

最新评论