软件工具——java开发sftp客户端上传下载文件

本文介绍如何使用Java语言开发一个SFTP客户端工具,以实现灵活的文件上传和下载功能。通过引入JSch库,配置读取类以及编写相关操作代码,可以在Windows环境下进行SFTP操作。
摘要由CSDN通过智能技术生成

上一篇博文:https://blog.csdn.net/qq_15903671/article/details/88681240

部署了一个linux的sftp-server。客户端工具众多且不受操作系统限制,但是想灵活的制作文件上传下载流程、定时触发、文件解析转存等操作,客户端工具就可能由于功能不够完整而使用不便了。

下面使用java语言做一个简单的sftp客户端操作工具来灵活的使用sftp。

开发环境:windows、IDEA、maven、java

一、引入maven依赖

在idea工程里找到pom.xml ,添加dependence标签

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<!-- sftp文件传输相关依赖-->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.48</version>
</dependency>

使用jsch依赖。

二、网上下载个操作类源码

package sshUtils;

import com.jcraft.jsch.*;

import java.io.*;
import java.util.*;

public class SFTPUtils {
    //private static Logger log = Logger.getLogger(SFTPUtils.class.getName());

    private String host;//服务器连接ip
    private String username;//用户名
    private String password;//密码
    private int port = 22;//端口号
    private ChannelSftp sftp = null;
    private Session sshSession = null;

    public SFTPUtils() {
    }

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

    public SFTPUtils(String host, String username, String password) {
        this.host = host;
        this.username = username;
        this.password = password;
    }

    /**
     * 通过SFTP连接服务器
     */
    public void connect() {
        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            //System.out.println("Session created.");
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            //System.out.println("Session connected.");
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            //System.out.println("Opening Channel.");
            sftp = (ChannelSftp) channel;
            System.out.println("Connected to " + host + " success.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void disconnect() {
        if (this.sftp != null) {
            if (this.sftp.isConnected()) {
                this.sftp.disconnect();
                System.out.println("sftp is closed already");
            }
        }
        if (this.sshSession != null) {
            if (this.sshSession.isConnected()) {
                this.sshSession.disconnect();
                System.out.println("sshSession is closed already");
            }
        }
    }

    /**
     * 批量下载文件
     *
     * @param remotePath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/)
     * @param localPath:本地保存目录(以路径符号结束,D:\Duansha\sftp\)
     * @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
     * @param fileEndFormat:下载文件格式(文件格式)
     * @param del:下载后是否删除sftp文件
     * @return
     */
    public List<String> batchDownLoadFile(String remotePath, String localPath,
                                          String fileFormat, String fileEndFormat, boolean del) {
        List<String> filenames = new ArrayList<String>();
        try {
            // connect();
            Vector v = listFiles(remotePath);
            // sftp.cd(remotePath);
            if (v.size() > 0) {
                System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size());
                Iterator it = v.iterator();
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值