java使用SFTP递归下载文件

远程的服务器上下载文件,由于sftp只能下载单独的文件,于是写了一个工具类来支持可以下载单独的文件或者下载一个文件夹下所有的文件。

项目中使用了hutool的Java工具类库中的JschUtil,Sftp,StrUtil。

hutool的Java工具类库学习链接简介 | Hutool

相关工具类库引用

gradle

    implementation 'com.jcraft:jsch:0.1.54'
    implementation 'cn.hutool:hutool-all:5.8.16'

maven

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>
<dependency>
	<groupId>com.jcraft</groupId>
	<artifactId>jsch</artifactId>
	<version>0.1.54</version>
</dependency>

工具类代码

package org.example;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ssh.Sftp;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpException;

import java.io.File;
import java.util.Vector;

public class DownloadFile {
    public static void downloadDir(Sftp sftp, String remotePath, String localPath) throws SftpException {
        ChannelSftp channelSftp = sftp.getClient();
        channelSftp.cd(remotePath);
        // 列出远程文件夹下的文件列表
        @SuppressWarnings("unchecked")
        Vector<ChannelSftp.LsEntry> fileList = channelSftp.ls(".");
        for (ChannelSftp.LsEntry file : fileList) {
            if (!".".equals(file.getFilename()) && !"..".equals(file.getFilename())) {
                String remoteFilePath = StrUtil.appendIfMissing(remotePath, "/") + file.getFilename();
                String localFilePath = StrUtil.appendIfMissing(localPath, "/") + file.getFilename();
                if (file.getAttrs().isDir()) {
                    File dir = new File(localFilePath);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    // 如果是文件夹,递归下载
                    downloadDir(sftp, remoteFilePath, localFilePath);
                } else {
                    File localFile = new File(localFilePath);
                    File parentFile = localFile.getParentFile();
                    if (!parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    // 如果是文件,直接下载
                    channelSftp.get(remoteFilePath, localFilePath);
                }
            }
        }
        sftp.close();
    }
}

使用demo

public class Demo {
    public static void main(String[] args) throws SftpException{
        String remoteFolderPath2 = "远程服务器下载文件地址";
        String localFolderPath2 = "本地存放下载文件地址";
        Sftp sftp= JschUtil.createSftp("远程服务器IP", 远程连接端口号, "服务器登录用户", "服务器登录密码");
        DownloadFile.downloadDir(sftp,remoteFolderPath2,localFolderPath2);

    }
}

您可以使用JSch库来实现在Java下载SFTP文件夹到本地的操作。下面是一个简单的示例代码: ```java import com.jcraft.jsch.*; public class SFTPDownloader { public static void main(String[] args) { String host = "your_sftp_host"; int port = 22; String username = "your_username"; String password = "your_password"; String remoteFolderPath = "/path/to/remote/folder"; String localFolderPath = "/path/to/local/folder"; 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(); downloadFolder(channelSftp, remoteFolderPath, localFolderPath); channelSftp.disconnect(); session.disconnect(); } catch (JSchException | SftpException e) { e.printStackTrace(); } } private static void downloadFolder(ChannelSftp channelSftp, String remoteFolderPath, String localFolderPath) throws SftpException { channelSftp.cd(remoteFolderPath); @SuppressWarnings("unchecked") java.util.Vector<ChannelSftp.LsEntry> fileList = channelSftp.ls("*"); for (ChannelSftp.LsEntry entry : fileList) { if (!entry.getAttrs().isDir()) { channelSftp.get(entry.getFilename(), localFolderPath + "/" + entry.getFilename()); } else if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) { downloadFolder(channelSftp, remoteFolderPath + "/" + entry.getFilename(), localFolderPath + "/" + entry.getFilename()); } } } } ``` 在上述代码中,您需要替换`your_sftp_host`、`your_username`、`your_password`、`/path/to/remote/folder`和`/path/to/local/folder`为适合您的实际情况的值。 这段代码创建了一个SFTP连接并下载指定SFTP服务器上的文件夹到本地文件夹。它使用递归的方式来处理文件夹内的内容,可以下载文件夹中的所有文件和子文件夹。 请确保您已经将JSch库添加到您的Java项目中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值