广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package cn.huge.module.ftp.utils;
 
import cn.huge.base.common.exception.MethodException;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.ftp.config.FtpFileConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.io.InputStream;
 
/**
 * @title: ftp配工具类
 * @description: ftp配工具类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
@Slf4j
@Component
public class FtpUtils {
 
    /**
     * 上传附件.
     * @param path 服务器存储路径(/home/ftp/huge-dyh)
     * @param fileName 文件名(1.jpg)
     * @param inputStream 文件流
     */
    public void upload(String path, String fileName, InputStream inputStream) {
        FTPClient ftpClient = new FTPClient();
        try {
            if (connect(ftpClient)) {
                // 检查文件目录是否存在,不存在创建目录
                if (this.checkDir(ftpClient, path)) {
                    // 检查是否上传成功
                    if (storeFile(ftpClient, fileName, inputStream)) {
                        disconnect(ftpClient);
                    }else{
                        disconnect(ftpClient);
                        throw new MethodException("连接ftp成功,上传文件失败!");
                    }
                }else{
                    disconnect(ftpClient);
                    throw new MethodException("连接ftp成功,检查文件目录不存在!");
                }
            }else{
                disconnect(ftpClient);
                throw new MethodException("连接ftp失败!");
            }
        } catch (Exception e) {
            disconnect(ftpClient);
            log.error("方法[FtpUtils.upload]调用异常:"+e, e);
            throw new MethodException("FtpUtils.upload", e);
        }
    }
 
    /**
     * 从文件服务器获取文件流.
     * @param filePath 文件存储完整路径(/home/ftp/huge-dyh/1.jpg)
     * @return {@link InputStream}
     * @throws IOException
     */
    public InputStream retrieveFileStream(String filePath) {
        if (StringUtils.isBlank(filePath)) {
            throw new MethodException("文件路径为空!");
        }
        log.info("读取文件路径[filePath]值" + filePath);
        FTPClient ftpClient = new FTPClient();
        try {
            if (connect(ftpClient)) {
                filePath = new String(filePath.getBytes("UTF-8"), "iso-8859-1");
                InputStream inputStream = ftpClient.retrieveFileStream(filePath);
                disconnect(ftpClient);
                return inputStream;
            }else{
                disconnect(ftpClient);
                throw new MethodException("连接ftp失败!");
            }
        } catch (Exception e) {
            disconnect(ftpClient);
            log.error("方法[FtpUtils.retrieveFileStream]调用异常:"+e, e);
            throw new MethodException("FtpUtils.retrieveFileStream", e);
        }
    }
 
    /**
     * 删除文件
     * @param filePath 文件存储完整路径(/home/ftp/huge-dyh/1.jpg)
     */
    public void deleteFile(String filePath) {
        try{
            String workingPath = filePath.substring(0, filePath.lastIndexOf("/"));
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
            deleteFile(workingPath, fileName);
        }catch (Exception e){
            log.error("方法[FtpUtils.deleteFile]调用异常:"+e, e);
            throw new MethodException("FtpUtils.deleteFile", e);
        }
    }
 
    /**
     * ftp操作-连接ftp
     * @param ftpClient ftp工具
     * @return boolean
     */
    private boolean connect(FTPClient ftpClient) {
        boolean flag = false;
        try {
            // 远程ftp需要加上,允许被动连接
            ftpClient.enterLocalPassiveMode();
            FTPClientConfig clientConfig = new FTPClientConfig();
            clientConfig.setLenientFutureDates(true);
            ftpClient.configure(clientConfig);
            // 连接ftp
            log.info("连接ftp"+ FtpFileConfig.getHostname(), FtpFileConfig.getPort());
            ftpClient.connect(FtpFileConfig.getHostname(), FtpFileConfig.getPort());
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                log.info("连接ftp成功!");
                boolean login = ftpClient.login(FtpFileConfig.getUsername(), FtpFileConfig.getPassword());
                log.info("登录ftp成功:{}",login);
                if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                    // 设置以二进制方式传输
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                    // 设置UTF-8编码
                    ftpClient.setControlEncoding("UTF-8");
                    //设置传输方式为流方式
                    ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
                    // 设置缓冲区大小
                    ftpClient.setBufferSize(1024);
 
                    log.info("登录ftp成功!");
                    flag = true;
                } else {
                    log.error("登录ftp失败,可能用户名或密码错误!");
                }
            }else{
                log.error("连接ftp失败,可能ip或端口错误!");
            }
            return flag;
        } catch (IOException e) {
            log.error("方法[FtpUtils.connect]调用异常:"+e, e);
            throw new MethodException("FtpUtils.connect", e);
        }
    }
 
    /**
     * ftp操作-检查文件目录不存在时,创建目录
     * @param ftpClient ftp工具
     * @param path 服务器存储路径(/home/ftp/huge-dyh)
     * @return String
     */
    public Boolean checkDir(FTPClient ftpClient, String path){
        try{
            log.info("上传路径为:" + path);
            Boolean flag = false;
            if (!ftpClient.changeWorkingDirectory(path)) {
                StringBuffer rootPath = new StringBuffer();
                rootPath.append(FtpFileConfig.getRootdir());
                // 创建文件目录
                path = path.replace(FtpFileConfig.getRootdir() + BaseConsts.SLANT, "");
                String[] paths = path.split(BaseConsts.SLANT);
                for (String filePath: paths){
                    if (StringUtils.isBlank(filePath)){
                        continue;
                    }else{
                        rootPath.append(BaseConsts.SLANT);
                        rootPath.append(filePath);
                        if (!ftpClient.changeWorkingDirectory(rootPath.toString())){
                            ftpClient.makeDirectory(rootPath.toString());
                        }
                    }
                }
                // 严谨一点,再检查文件目录是否存在
                if (ftpClient.changeWorkingDirectory(rootPath.toString())) {
                    flag = true;
                }
            }else{
                flag = true;
            }
            return flag;
        }catch (Exception e){
            log.error("方法[FtpUtils.makeDir]调用异常:"+e, e);
            throw new MethodException("FtpUtils.makeDir", e);
        }
    }
 
    /**
     * ftp操作-上传文件到ftp
     * @param ftpClient ftp工具
     * @param fileName 文件名称(1.jpg)
     * @param fileInputStream 文件流
     * @return boolean
     */
    private boolean storeFile(FTPClient ftpClient, String fileName, InputStream fileInputStream) {
        boolean flag = false;
        try {
            fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
            log.info("上传文件名为:" + fileName);
            boolean b = ftpClient.storeFile(fileName, fileInputStream);
            String status = ftpClient.getStatus();
            log.info("上传状态:{},{}" ,b, status);
            int reply = ftpClient.getReplyCode();
            if (FTPReply.isPositiveCompletion(reply)) {
                flag = true;
                log.info("上传文件成功!");
            }else{
                log.error("上传文件失败!请求响应码[reply]值为:" + reply);
            }
            fileInputStream.close();
            return flag;
        } catch (IOException e) {
            log.error("方法[FtpUtils.storeFile]调用异常:"+e, e);
            throw new MethodException("FtpUtils.storeFile", e);
        }
    }
 
    /**
     * ftp操作-删除附件.
     * @param path 服务器存储路径(/home/ftp/huge-dyh)
     * @param fileName 附件名称(1.jpg)
     */
    public void deleteFile(String path, String fileName) {
        FTPClient ftpClient = new FTPClient();
        try{
            connect(ftpClient);
            if (ftpClient.changeWorkingDirectory(path)) {
                // 删除文件
                ftpClient.dele(fileName);
                disconnect(ftpClient);
            }else {
                disconnect(ftpClient);
                throw new MethodException("文件路径不存在!");
            }
        }catch (Exception e){
            disconnect(ftpClient);
            log.error("方法[FtpUtils.deleteFile]调用异常:"+e, e);
            throw new MethodException("FtpUtils.deleteFile", e);
        }
    }
 
    /**
     * ftp操作-断开ftp连接.
     * @param ftpClient ftp工具
     */
    private void disconnect(FTPClient ftpClient) {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                log.error("方法[FtpUtils.disconnect]调用异常:"+e, e);
                throw new MethodException("FtpUtils.disconnect", e);
            }
        }
    }
 
}