liusheng
2025-02-27 e00ef99886b9ab84f39c81432f8c7640e93026f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.smartor.common;
 
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.Vector;
 
@Slf4j
@Component
public class FtpService {
 
    @Value("${FTP_SERVER}")
    private String FTP_SERVER;
 
    @Value("${FTP_USERNAME}")
    private String FTP_USERNAME;
 
    @Value("${FTP_PASSWORD}")
    private String FTP_PASSWORD;
 
 
    public void downloadFolder(String remoteDir, String localDir) {
        Session session = null;
        ChannelSftp channelSftp = null;
 
        try {
            // 1. 创建 JSch 实例并设置会话
            JSch jsch = new JSch();
            session = jsch.getSession(FTP_USERNAME, FTP_SERVER, 8094);
            session.setPassword(FTP_PASSWORD);
 
            // 跳过主机密钥检查
            session.setConfig("StrictHostKeyChecking", "no");
 
            // 2. 连接到 SFTP 服务器
            session.connect();
            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
 
            // 3. 开始递归下载
            downloadDirectory(channelSftp, remoteDir, localDir);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭连接
            if (channelSftp != null) {
                channelSftp.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
    }
 
    /**
     * 递归下载远程目录到本地
     *
     * @param channelSftp SFTP 通道
     * @param remoteDir   远程目录路径
     * @param localDir    本地目录路径
     * @throws Exception 异常
     */
    private static void downloadDirectory(ChannelSftp channelSftp, String remoteDir, String localDir) throws Exception {
        // 确保本地目录存在
        File localDirectory = new File(localDir);
        if (!localDirectory.exists()) {
            localDirectory.mkdirs();
        }
 
        // 列出远程目录中的所有文件和文件夹
        Vector<ChannelSftp.LsEntry> entries = channelSftp.ls(remoteDir);
 
        for (ChannelSftp.LsEntry entry : entries) {
            String fileName = entry.getFilename();
 
            // 跳过 "." 和 ".."
            if (".".equals(fileName) || "..".equals(fileName)) {
                continue;
            }
 
            String remoteFilePath = remoteDir + "/" + fileName;
            String localFilePath = localDir + File.separator + fileName;
 
            if (entry.getAttrs().isDir()) {
                // 如果是文件夹,递归下载
                downloadDirectory(channelSftp, remoteFilePath, localFilePath);
            } else {
                // 如果是文件,覆盖下载
                File localFile = new File(localFilePath);
 
                // 如果本地文件已存在,先删除
                if (localFile.exists()) {
                    localFile.delete();
                }
 
                try (FileOutputStream fos = new FileOutputStream(localFile)) {
                    channelSftp.get(remoteFilePath, fos);
                    log.error("文件下载成功:{}", remoteFilePath);
                } catch (Exception e) {
                    System.err.println("文件下载失败: " + remoteFilePath);
                    e.printStackTrace();
                }
            }
        }
    }
//
//    @Test
//    public void aa() {
//        downloadFolder("/", "D:\\ruoyi\\uploadPath\\upload\\vadio\\voice");
//    }
}