Java从sftp服务器上传与下载文件

一、背景

业务需要从sftp服务器上上传、下载、删除文件等功能,通过查阅资料及手动敲打代码,实现了操作sftp的基本功能,有需求的小伙伴可以看看具体的实现过程。

二、sftp介绍

摘自百度百科:SSH文件传输协议,是一种数据流链接,提供文件访问、传输和管理功能的网络传输协议。

SFTP允许用户以文件操作的方式(如文件的增、删、改、查、传送等)与另一主机相互通信。

三、具体代码实现

1、引入以下依赖


<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>

2、编写一个sftp工具类

含以下方法:

*获取一个sftp通道

*关闭sftp连接

*下载文件

*上传文件

*删除文件

其他方法自行拓展


import com.jcraft.jsch.*;
import java.io.*;
import java.util.Properties;

/**
 * sftp工具类,包含以下功能:
 * 获取sftp链接
 * 关闭sftp链接
 * 下载文件
 * 上传文件
 * 删除文件
 * 查找文件
 * 更多功能自行拓展
 */
public class SftpUtil {

    /**
     * 获取一个sftp链接
     * @param host sftp服务器ip
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @return 返回ChannelSftp
     * @throws Exception 获取通道时的异常
     */
    public static ChannelSftp getSftpChannel(String host, Integer port, String username, String password) throws Exception{
        Session session;
        Channel channel = null;
        JSch jSch = new JSch();
        try {
            session = jSch.getSession(username, host, port);
            session.setPassword(password);

            // 配置链接的属性
            Properties properties = new Properties();
            properties.setProperty("StrictHostKeyChecking","no");
            session.setConfig(properties);

            // 进行sftp链接
            session.connect();

            // 获取通信通道
            channel = session.openChannel("sftp");
            channel.connect();
        } catch (JSchException e) {
            e.printStackTrace();
            throw e;
        }
        return (ChannelSftp)channel;
    }

    /**
     * 上传文件
     * @param channelSftp sftp通道
     * @param localFile 本地文件
     * @param remoteFile 远程文件
     */
    public static void upload(ChannelSftp channelSftp, String localFile, String remoteFile) throws Exception{
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(localFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        
        channelSftp.put(inputStream, remoteFile);
        
        inputStream.close();
        
    }

    /**
     * 从sftp服务器上下载文件
     * @param channelSftp sftp通道
     * @param remoteFile 远程文件
     * @param localFile 本地文件
     */
    public static void download(ChannelSftp channelSftp, String remoteFile, String localFile) throws Exception{
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(localFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        
        channelSftp.get(remoteFile, outputStream);
        
        outputStream.close();
    }

    /**
     * 删除文件
     * @param channelSftp sftp通道
     * @param remoteFile 远程文件
     */
    public static void deleteFile(ChannelSftp channelSftp, String remoteFile) throws Exception{
        try {
            channelSftp.rm(remoteFile);
        } catch (SftpException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 关闭sftp链接
     * @param channelSftp sftp通道
     * @throws Exception 关闭session的异常
     */
    public static void closeSession(ChannelSftp channelSftp) throws Exception{
        if(channelSftp == null){
            return;
        }

        Session session = null;
        try {
            session = channelSftp.getSession();
        } catch (JSchException e) {
            e.printStackTrace();
            throw e;
        }finally {
            if(session != null){
                session.disconnect();
            }
        }

    }

}

3、测试连接、上传文件、下载文件、删除文件、关闭连接功能


public class TestSftp {

    public static void main(String[] args) throws Exception{
        String host = "your sftp host";
        Integer port = 22;
        String username = "your sftp user";
        String password = "your sftp password";

        // 获取sftp通道
        ChannelSftp channelSftp = SftpUtil.getSftpChannel(host, port, username, password);

        // 上传文件
        String local = "D:\\local\\upload.txt";
        String remote = "wly/upload.txt";
        SftpUtil.upload(channelSftp, local, remote);

        // 下载文件
        String remote2 = "wly/upload.txt";
        String local2 = "D:\\local\\test.txt";
        SftpUtil.download(channelSftp, remote2, local2);

        // 删除文件
        /*String remote3 = "wly/upload.txt";
        SftpUtil.deleteFile(channelSftp, remote3);*/

        // 关闭链接
        SftpUtil.closeSession(channelSftp);
    }

}

经过测试,能够正常连接上ftp服务器,并且上传下载文件,最后关闭连接。

四、参考连接

https://blog.csdn.net/MyBlogHiHi/article/details/119905438

版权声明:本文为博主作者:明天再去学习原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/Staba/article/details/129476074

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2024年1月6日
下一篇 2024年1月6日

相关推荐