FTPClientHelper.java
10.3 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
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import java.io.*;
/**
* ftp客户端辅助bean
*/
@Slf4j
public class FTPClientHelper {
private FTPClientPool ftpClientPool;
private String tempPath;
private int retryCount = 3;
public void setFtpClientPool(FTPClientPool ftpClientPool) {
this.ftpClientPool = ftpClientPool;
}
/**
* 下载 remote文件流
*
* @param remote 远程文件
*
* @return 字节数据
*/
public byte[] retrieveFileStream(String remote) {
FTPClient client = null;
ByteArrayOutputStream out = null;
try {
log.info("开始获取FTPClient对象");
client = ftpClientPool.borrowObject();
log.info("结束获取FTPClient对象");
out = new ByteArrayOutputStream();
log.info("开始获取文件");
boolean result = client.retrieveFile(remote, out);
log.info("结束获取文件" + result);
if (result) {
return out.toByteArray();
}
} catch (Exception e) {
e.printStackTrace();
log.error("下载文件失败", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClientPool.returnObject(client);
}
return null;
}
/**
* 创建目录 单个不可递归
*
* @param pathname 目录名称
*
* @return
*/
public boolean makeDirectory(String pathname) throws Exception {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
return client.makeDirectory(pathname);
} finally {
ftpClientPool.returnObject(client);
}
}
/**
* 删除目录,单个不可递归
*
* @param pathname
*
* @return
*/
public boolean removeDirectory(String pathname) throws Exception {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
return client.removeDirectory(pathname);
} finally {
ftpClientPool.returnObject(client);
}
}
/**
* 删除文件 单个 ,不可递归
*
* @param pathname
*
* @return
*/
public boolean deleteFile(String pathname) throws Exception {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
return client.deleteFile(pathname);
} finally {
ftpClientPool.returnObject(client);
}
}
/**
* 上传文件
*
* @param remote
* @param local
*
* @return
*/
public boolean storeFile(String remote, InputStream local) {
Exception throwException = null;
for (int i = 0; i < retryCount; i++) { // 尝试三次 如果三次不成功那么存储到本地
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
boolean result = client.storeFile(remote, local);
if (result) {
return result;
}
} catch (Exception e) {
e.printStackTrace();
throwException = e;
} finally {
if (local != null) {
try {
local.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClientPool.returnObject(client);
}
}
log.error("通过ftp存储文件" + remote + "时发生异常 ,开始尝试存储到本地路径" + tempPath + "下", throwException);
return storeTemp(remote, local);
}
public boolean isExist(String remote) {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
int count = client.list(remote);
if (count == 0) {
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ftpClientPool.returnObject(client);
}
return false;
}
public boolean storeFile(String remote, byte[] content) {
Exception throwException = null;
for (int i = 0; i < retryCount; i++) { // 尝试三次 如果三次不成功那么存储到本地
Long startTime = System.currentTimeMillis();
FTPClient client = null;
ByteArrayInputStream bis = null;
try {
client = ftpClientPool.borrowObject();
String parentPath = getParentPath(remote);
boolean makeDirectoryResult = client.makeDirectory(parentPath);
if (!makeDirectoryResult) {//如果创建失败 可能是不支持一次创建多级目录
makeDirectorys(client, parentPath);
}
bis = new ByteArrayInputStream(content);
boolean result = client.storeFile(remote, bis);
if (result) {
return result;
} else {
log.error("通过ftp存储文件" + remote + "时未发生异常 ,但存储失败");
return result;
}
} catch (Exception e) {
e.printStackTrace();
throwException = e;
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClientPool.returnObject(client);
Long endTime = System.currentTimeMillis();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
// log.error("通过ftp存储文件" + remote + "时发生异常 ,开始尝试存储到本地路径" + tempPath + "下", throwException);
log.error("通过ftp存储文件" + remote + "时发生异常");
// return storeTemp(remote, content);
return false;
}
private boolean makeDirectorys(FTPClient ftpclient, String remote) throws Exception {
File file = new File(remote);
if (!file.getPath().contains(File.separator)) {
ftpclient.makeDirectory(file.getPath());
} else {
makeDirectorys(ftpclient, file.getParent());
ftpclient.makeDirectory(file.getPath());
}
return true;
}
private boolean storeTemp(String remote, InputStream local) {
FileOutputStream fos = null;
try {
File file = new File(tempPath, remote);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
int bytesWritten = 0;
int byteCount = 0;
byte[] bytes = new byte[1024];
while ((byteCount = local.read(bytes)) != -1) {
fos.write(bytes, bytesWritten, byteCount);
bytesWritten += byteCount;
}
return true;
} catch (Exception e) {
log.error("存储到本地路径" + tempPath + "下失败", e);
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (local != null) {
local.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private boolean storeTemp(String remote, byte[] content) {
FileOutputStream fos = null;
try {
File file = new File(tempPath, remote);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
fos.write(content);
fos.flush();
return true;
} catch (Exception e) {
log.error("存储到本地路径" + tempPath + "下失败", e);
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public InputStream getInputStream(FileInputStream fileInput) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = -1;
InputStream inputStream = null;
try {
while ((n = fileInput.read(buffer)) != -1) {
baos.write(buffer, 0, n);
}
byte[] byteArray = baos.toByteArray();
inputStream = new ByteArrayInputStream(byteArray);
return inputStream;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String getParentPath(String remote) {
File f = new File(remote);
return f.getParent();
}
public String getTempPath() {
return tempPath;
}
public void setTempPath(String tempPath) {
this.tempPath = tempPath;
}
public boolean deleteItem(String remote) {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
} catch (Exception e) {
e.printStackTrace();
}
boolean isDelete = false;
try {
isDelete = client.deleteFile(remote);
} catch (IOException e) {
e.printStackTrace();
}
ftpClientPool.returnObject(client);
return isDelete;
}
}