xiangpei
2024-09-11 6460fb8ee91d6882269028c599d518611aead76b
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package org.dromara.system.uitil;
 
import cn.hutool.extra.ftp.Ftp;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.dromara.system.domain.properties.FtpConfig;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 实现FTP文件上传和文件下载
 */
public class FtpApche {
    private static FTPClient ftpClient = new FTPClient();
    private static String encoding = System.getProperty("file.encoding");
    /**
     * 超时时间
     */
    private static final int DEFAULT_TIMEOUT = 60 * 1000;
 
    /**
     * 连接到ftp
     */
    public static void connect(FtpConfig config) throws IOException {
        setTimeout();
        try {
            ftpClient.connect(config.getUrl(), 21);
        } catch (UnknownHostException e) {
            throw new IOException("Can't find FTP server :" + "21");
        }
 
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            disconnect();
            throw new IOException("Can't connect to server :" + "21");
        }
 
        if (!ftpClient.login(config.getUsername(), config.getPasswd())) {
            disconnect();
            throw new IOException("Can't login to server :" + "21");
        }
 
        // set data transfer mode.
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
 
        // Use passive mode to pass firewalls.
        ftpClient.enterLocalPassiveMode();
    }
 
    /**
     * 设置超时时间
     */
    private static void setTimeout() {
        ftpClient.setDefaultTimeout(DEFAULT_TIMEOUT);
        ftpClient.setConnectTimeout(DEFAULT_TIMEOUT);
        ftpClient.setDataTimeout(DEFAULT_TIMEOUT);
    }
 
    /**
     * ftp是否处于连接状态,是连接状态返回<tt>true</tt>
     *
     * @return boolean  是连接状态返回<tt>true</tt>
     */
    public static boolean isConnected() {
        return ftpClient.isConnected();
    }
 
    /**
     * 关闭ftp连接
     */
    public static void disconnect() {
        if (null != ftpClient && ftpClient.isConnected()) {
            try {
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (IOException ex) {
                // do nothing
            }
        }
    }
 
    /**
     * 改变工作目录
     *
     * @param dir ftp服务器上目录
     * @return boolean 改变成功返回true
     */
    public static boolean changeWorkingDirectory(String dir) {
        try {
            return ftpClient.changeWorkingDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * Description: 向FTP服务器上传文件
     *
     * @return 成功返回true,否则返回false
     */
    public static boolean uploadFile(FtpConfig config, MultipartFile file, String name) throws IOException {
        boolean result = false;
        InputStream input = file.getInputStream();
        try {
            connect(config);
            ftpClient.setBufferSize(1024 * 1024);
            result = ftpClient.storeFile(new String(name.getBytes(encoding), "iso-8859-1"), input);
            if (result) {
                System.out.println("上传成功!");
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ftpClient.logout();
            ftpClient.disconnect();
        }
        return result;
    }
 
    /**
     * 根据文件ftp路径名称删除文件
     *
     * @param ftpFileName 文件ftp路径名称
     */
    public static void deleteFile(String ftpFileName) throws IOException {
        int dele = ftpClient.dele(ftpFileName);
        try {
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * 根据uploadFile返回的路径,从ftp下载文件到指定输出流中
     *
     * @param filename 文件名称
     */
    public static InputStream downloadFileFromDailyDir(String filename) throws IOException {
        return ftpClient.retrieveFileStream(filename);
    }
 
    public static List<String> downloadList(FtpConfig config) throws IOException {
        connect(config);
        List<String> list = new ArrayList<>();
        FTPFile[] ftpFiles = ftpClient.listFiles();
        if (ftpFiles.length == 0) {
            return list;
        }
        for (FTPFile ftpFile : ftpFiles) {
            list.add(ftpFile.getName());
        }
        return list;
    }
 
    /**
     * 根据名称获取文件,以字节数组返回
     *
     * @param ftpPath  FTP服务器文件相对路径,例如:test/123
     * @param fileName 文件名,例如:test.xls
     * @return byte[] 字节数组对象
     */
    public static byte[] getFileBytesByName(InputStream is) throws IOException {
        //创建byte数组输出流
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        try {
            try {
                byte[] buffer = new byte[1024 * 1024 * 4];
                int len = -1;
                while ((len = is.read(buffer, 0, 1024 * 1024 * 4)) != -1) {
                    byteStream.write(buffer, 0, len);
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteStream.toByteArray();
    }
 
}