FTPClientHelper.java
16.8 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package org.nafmii.common.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.nafmii.common.constant.MessageEnum;
import org.nafmii.common.exception.BusinessException;
import org.nafmii.common.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* @author by zhangbr
* @date 2020/7/24.
*/
@Slf4j
public class FTPClientHelper implements AutoCloseable {
@Resource
private FTPClientConfigure configure;
private static FTPClientHelper ftpClientHelper;
private static FTPClientFactory factory;
private static final String SUB_SPOT = ".";
/**
* 初始化设置
*/
@PostConstruct
public boolean init() {
factory = new FTPClientFactory(configure);
ftpClientHelper = this;
return true;
}
/**
* 获取一个连接对象
* @return FTPClient
*/
private static FTPClient getClient() {
return factory.create();
}
/**
* 列出目录下的所有文件
* @param pathName 路径
* @return FTPFile[]
*/
public static FTPFile[] listFiles(String pathName) {
FTPClient client = getClient();
try {
return client.listFiles(pathName);
} catch (IOException e) {
log.info(e.getMessage());
}finally {
releaseClient(client);
}
return null;
}
/**
* 下载 remote文件流
* @param form ftp资源下载
* @return boolean
*/
public static boolean retrieveFile(FileDownLoadForm form) {
String remote = form.encodeFileId();
if (StringUtils.isEmpty(remote)){
log.info("==============下载FTP文件失败:remote:{}=============", remote, remote);
throw new BusinessException(MessageEnum.FILE_DOWN_FAIL.getCode(), MessageEnum.FILE_DOWN_FAIL.getRemarks());
}
log.info("==============开始下载FTP文件:{}=============", remote);
long startTime = System.currentTimeMillis();
FTPClient client = getClient();
OutputStream outputStream = null;
try {
if (!existFile(client, remote)){
log.info("==============下载FTP文件失败:未找到文件:{}=============", remote);
throw new BusinessException(MessageEnum.FILE_DOWN_FAIL.getCode(), MessageEnum.FILE_DOWN_FAIL.getRemarks());
}
outputStream = RequestUtils.getResponse().getOutputStream();
boolean result = client.retrieveFile(remote, outputStream);
long endTime = System.currentTimeMillis();
log.info("==============下载FTP文件:{}完成,result:{},用时:{}=============", remote, result, (endTime - startTime) + "ms");
return result;
} catch (IOException e) {
log.info(e.getMessage());
throw new BusinessException(MessageEnum.FILE_DOWN_FAIL.getCode(), MessageEnum.FILE_DOWN_FAIL.getRemarks());
} finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
log.info(e.getMessage());
}
}
releaseClient(client);
}
}
/**
* 下载 remote文件流
* @param remote ftp资源文件路径
* @return byte[]
*/
public static InputStream retrieveFileStream(String remote) {
log.info("==============开始下载FTP文件:{}=============", remote);
long startTime = System.currentTimeMillis();
FTPClient client = getClient();
InputStream in = null;
try {
if (!existFile(client, remote)){
log.info("==============下载FTP文件失败:未找到文件:{}=============", remote);
throw new BusinessException(MessageEnum.FILE_DOWN_FAIL.getCode(), MessageEnum.FILE_DOWN_FAIL.getRemarks());
}
in = client.retrieveFileStream(remote);
long endTime = System.currentTimeMillis();
log.info("==============下载FTP文件:{}完成,用时:{}=============", remote, (endTime - startTime) + "ms");
} catch (IOException e) {
log.info(e.getMessage());
} finally {
releaseClient(client);
}
return in;
}
/**
* 根据业务分类上传文件(同步上传)
* @param business 业务模块
* @param inputStream 文件流
* @return String
*/
public static String uploadFile(String business, InputStream inputStream, String fileName) {
String remotePath;
FTPClient client = getClient();
try {
remotePath = makeDirectory(client, business);
remotePath += fileName;
upload(client, remotePath, inputStream);
} catch (IOException e) {
throw new BusinessException(-102, "附件上传失败!");
} finally {
releaseClient(client);
}
return remotePath;
}
/**
* 根据业务分类上传文件(同步上传)
* @param business 业务模块
* @param file 文件资源
* @return String
*/
public static String uploadFile(String business, File file, String fileName) {
if (file == null || StringUtils.isEmpty(business) || StringUtils.isEmpty(fileName)){
log.info("======上传附件异常======business:{},fileName:{}", business, fileName);
throw new BusinessException(-101, "附件上传失败!");
}
String remotePath;
FTPClient client = getClient();
InputStream inputStream;
try {
remotePath = makeDirectory(client, business);
remotePath += fileName;
inputStream = new FileInputStream(file);
upload(client, remotePath, inputStream);
} catch (IOException e) {
throw new BusinessException(-102, "附件上传失败!");
} finally {
releaseClient(client);
}
return remotePath;
}
/**
* 根据路径上传文件(异步上传)
* @param filePathName ftp附件上传路径
* @param file 文件资源
* @return boolean
*/
public static boolean uploadFile(String filePathName, File file) {
if (file == null || StringUtils.isEmpty(filePathName)){
log.info("======上传附件异常======filePathName:{}", filePathName);
throw new BusinessException(-103, "附件上传失败!");
}
FTPClient client = getClient();
InputStream inputStream;
boolean flag;
try {
inputStream = new FileInputStream(file);
flag = upload(client, filePathName, inputStream);
} catch (IOException e) {
log.info(e.getMessage());
throw new BusinessException(-104, "附件上传失败!");
} finally {
releaseClient(client);
}
return flag;
}
/**
* 多文件上传
* @param business 业务模块
* @param files 文件集合
* @return List<FileVO>
*/
public static List<FileVO> uploadFiles(String business, MultipartFile[] files){
List<FileVO> fileList = new ArrayList<>();
FTPClient client = getClient();
try {
String remotePath = makeDirectory(client, business);
for (MultipartFile file : files) {
try {
//上传文件
String fileName = file.getOriginalFilename();
if (StringUtils.isEmpty(fileName)){
continue;
}
String suffix = fileName.substring(fileName.lastIndexOf(SUB_SPOT));
if (StringUtils.isEmpty(suffix)){
continue;
}
String newFileName = UUID.randomUUID().toString().replace("-", "") + suffix;
String remoteFilePath = remotePath + newFileName;
InputStream inputStream = file.getInputStream();
upload(client, remoteFilePath, inputStream);
inputStream.close();
fileList.add(new FileVO(remoteFilePath, newFileName));
} catch (IOException e) {
throw new BusinessException(MessageEnum.FILE_UPLOAD_FAIL.getCode(), MessageEnum.FILE_UPLOAD_FAIL.getRemarks());
}
}
} catch (IOException e) {
throw new BusinessException(-100, "FTP目录生成失败!");
} finally {
releaseClient(client);
}
return fileList;
}
private static boolean upload(FTPClient client, String filePathName, InputStream inputStream){
try {
log.info("==============FTP文件:{},开始上传=============", filePathName);
long start = System.currentTimeMillis();
boolean result = client.storeFile(filePathName, inputStream);
long end = System.currentTimeMillis();
log.info("==============FTP文件:{},上传完成时间:{}=============", filePathName, (end - start) + "ms");
return result;
} catch (IOException e) {
throw new BusinessException(MessageEnum.FILE_UPLOAD_FAIL.getCode(), MessageEnum.FILE_UPLOAD_FAIL.getRemarks());
}finally {
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
log.info(e.getMessage());
}
}
}
}
/**
* 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
* @param business 业务模块
* @return String
*/
public static String makeDirectory(String business){
String remotePath;
FTPClient client = getClient();
try {
remotePath = makeDirectory(client, business);
} catch (IOException e) {
throw new BusinessException(-100, "文件目录生成失败!");
} finally {
releaseClient(client);
}
return remotePath;
}
/**
* 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
* @param client client
* @param business 业务模块
* @return String
* @throws IOException e
*/
private static String makeDirectory(FTPClient client, String business) throws IOException {
log.info("==============开始生成FTP目录:{}=============", business);
long startTime = System.currentTimeMillis();
String directory = getRemoteBusinessPath(business);
// 如果远程目录不存在,则递归创建远程服务器目录
if (!directory.equalsIgnoreCase(File.separator) && !changeWorkingDirectory(client, directory)) {
int start;
int end;
if (directory.startsWith(File.separator)) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf(File.separator, start);
String path = "";
String paths = "";
while (true) {
String subDirectory = new String(directory.substring(start, end).getBytes("GBK"), StandardCharsets.ISO_8859_1);
path = path + File.separator + subDirectory;
if (!existFile(client, path)) {
if (createDirectory(client, subDirectory)) {
changeWorkingDirectory(client, subDirectory);
} else {
log.info("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(client, subDirectory);
}
} else {
changeWorkingDirectory(client, subDirectory);
}
paths = paths + File.separator + subDirectory;
start = end + 1;
end = directory.indexOf(File.separator, start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
long endTime = System.currentTimeMillis();
log.info("==============生成FTP目录:{}完成,用时:{}=============", business, (endTime - startTime) + "ms");
return directory;
}
/**
* 根据业务模块创建远程文件路径
* @param business 业务模块
* @return String
*/
public static String getRemoteBusinessPath(String business){
if (StringUtils.isEmpty(business)){
throw new BusinessException(-100, "业务模块不能为空!");
}
String filePath = StringUtils.isEmpty(ftpClientHelper.configure.getPrefix()) ? "" : ftpClientHelper.configure.getPrefix();
if (!filePath.startsWith(File.separator)){
filePath = File.separator + filePath;
}
if (!filePath.endsWith(File.separator)){
filePath += File.separator;
}
filePath += business + File.separator;
filePath += new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + File.separator;
return filePath;
}
/**
* 判断ftp服务器文件是否存在
* @param path path
* @return boolean
*/
public static boolean existFile(String path){
log.info("==============校验FTP文件是否存在:{}=============", path);
long startTime = System.currentTimeMillis();
boolean flag;
FTPClient client = getClient();
try {
flag = existFile(client, path);
} finally {
releaseClient(client);
}
long endTime = System.currentTimeMillis();
log.info("==============校验FTP文件:{}完成,结果:{},用时:{}=============", path, flag, (endTime - startTime) + "ms");
return flag;
}
/**
* 判断ftp服务器文件是否存在
* @param client client
* @param path path
* @return boolean
*/
private static boolean existFile(FTPClient client, String path){
boolean flag = false;
FTPFile[] ftpFileArr = new FTPFile[0];
try {
ftpFileArr = client.listFiles(path);
} catch (IOException e) {
log.info(e.getMessage());
}
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
/**
* 改变目录路径
* @param client client
* @param directory directory
* @return boolean
*/
private static boolean changeWorkingDirectory(FTPClient client, String directory) {
try {
return client.changeWorkingDirectory(directory);
} catch (IOException ioe) {
log.info(ioe.getMessage());
throw new BusinessException(-100, "改变目录路径异常!");
}
}
/**
* 创建目录 单个不可递归
* @param client client
* @param pathName ftp路径
* @return boolean
* @throws IOException e
*/
private static boolean createDirectory(FTPClient client, String pathName) throws IOException {
return client.makeDirectory(pathName);
}
/**
* 删除目录,单个不可递归
* @param pathName ftp路径
* @return boolean
* @throws Exception e
*/
public static boolean removeDirectory(String pathName) throws Exception {
FTPClient client = getClient();
try {
return client.removeDirectory(pathName);
} finally {
releaseClient(client);
}
}
/**
* 删除文件 单个 ,不可递归
* @param pathName ftp资源文件路径
* @return boolean
* @throws Exception e
*/
public static boolean deleteFile(String pathName) throws Exception {
FTPClient client = getClient();
try {
return client.deleteFile(pathName);
} finally {
releaseClient(client);
}
}
/**
* 释放连接
* @param client client
*/
private static void releaseClient(FTPClient client) {
if (client == null) {
return;
}
try {
client.logout();
client.disconnect();
} catch (Exception io) {
log.info(io.getMessage());
}
}
@Override
public void close() throws Exception {
// TODO Auto-generated method stub
log.info("---Resources Closed---.");
}
}