FTPDaemon.java
11.2 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
package com.sitech.util.upload;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDaemon extends Thread {
private boolean run = true;
private boolean passiveMode = false;
private boolean doIt = false; //是否执行
private int type; //操作类型
private String returnValue = null; //返回值
private AFtpRemoteFile[] ftpRemoteFiles = null;
private AFtpRemoteFile ftpRemoteFile = null;
private String[] args = null; //参数
private InputStream inputStream = null;
private OutputStream outputStream = null;
private FTPClient ftpClient = null; //Apache FTP客户端
private Exception exception = null; //异常
public final static int LOGIN = 1;
public final static int LOGOUT = 2;
public final static int ENTRY_PASSIVE_MODE = 3;
public final static int GET_REMOTE_HOST_IP = 4;
public final static int GET_ENCODING = 5;
public final static int REMOVE_DIR = 6;
public final static int MAKE_DIR = 7;
public final static int CHANGE_DIR = 8;
public final static int PWD = 9;
public final static int LIST = 10;
public final static int GET_REMOTE_FILE = 11;
public final static int NLST = 12;
public final static int STORE = 13;
public final static int RETRIVE = 14;
public final static int RETRIVE_BY_STREAM = 15;
public final static int STORE_BY_STREAM = 16;
public final static int COMPLETE = 17;
public final static int REMOVE_FILE = 18;
public final static int RENAME_FILE = 19;
public void run() {
while (run) {
try {
sleep(100);
} catch (InterruptedException e) {
}
if (!run)
continue;
if(!doIt){
continue;
}
try{
switch (type) {
case LOGIN: {
ftpClient = new FTPClient();
if(args[4]!=null && args[4].length()>0){
ftpClient.setControlEncoding(args[4]);
}
ftpClient.connect(args[0], Integer.parseInt(args[1]));
ftpClient.setSoTimeout(Integer.parseInt(args[5]));
if(!ftpClient.login(args[2], args[3])){
throw new Exception("login fail, please check user and password");
}
break;
}
case LOGOUT: {
if(ftpClient != null){
try {
ftpClient.logout();
}catch(Exception e){
}
if ("true".equalsIgnoreCase(args[0])) {
try {
ftpClient.disconnect();
} catch (Exception e) {
}
try {
ftpClient.disconnect();
}catch(Exception e) {
}
}else {
try {
ftpClient.disconnect();
}catch(Exception e) {
}
try {
ftpClient.disconnect();
} catch (Exception e) {
}
}
ftpClient = null;
}
break;
}
case ENTRY_PASSIVE_MODE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before entryPassiveMode");
}else{
ftpClient.enterLocalPassiveMode();
passiveMode = true;
}
break;
}
case GET_REMOTE_HOST_IP: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before getRemoteHostIp");
}else{
returnValue = ftpClient.getRemoteAddress().getHostAddress();
}
break;
}
case GET_ENCODING: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before getEncoding");
}else{
returnValue = ftpClient.getControlEncoding();
}
break;
}
case REMOVE_DIR: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before rmdir");
}else{
if(ftpClient.removeDirectory(args[0])){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case REMOVE_FILE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before rmfile");
}else{
if(ftpClient.deleteFile(args[0])){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case MAKE_DIR: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before mkdir");
}else{
if(ftpClient.makeDirectory(args[0])){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case CHANGE_DIR: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before chdir");
}else{
if(ftpClient.changeWorkingDirectory(args[0])){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case PWD: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before pwd");
}else{
returnValue = ftpClient.printWorkingDirectory();
}
break;
}
case LIST: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before list");
}else{
String currdir = ftpClient.printWorkingDirectory();
if (currdir.endsWith("/") == false) {
currdir = currdir + "/";
}
FTPFile[] rfileList = ftpClient.listFiles(currdir);
if (rfileList != null && rfileList.length > 0) {
ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
for (int i=0; i<rfileList.length; i++){
ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
}
}
}
break;
}
case GET_REMOTE_FILE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before getAFtpRemoteFile");
}else{
FTPFile[] rfileList = ftpClient.listFiles(args[0]);
if(rfileList==null || rfileList.length<1 || rfileList[0]==null){
ftpRemoteFile = null;
}else{
ftpRemoteFile = new AFtpRemoteFile(rfileList[0], ftpClient, null);
}
}
break;
}
case NLST: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before nlst");
}else{
String names[] = ftpClient.listNames(args[0]);
String currdir = ftpClient.printWorkingDirectory();
if (currdir.endsWith("/") == false) {
currdir = currdir + "/";
}
// 快速遍历,不获取文件属性。
if ("true".equalsIgnoreCase(args[1])) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis() + Long.parseLong(args[3]));
ftpRemoteFiles = new AFtpRemoteFile[names.length];
for (int i=0; i<names.length; i++) {
FTPFile fFile = new FTPFile();
fFile.setGroup("");
fFile.setName(names[i]);
fFile.setSize(0);
fFile.setTimestamp(c);
fFile.setType(0);
fFile.setUser("Unknown");
ftpRemoteFiles[i] = new AFtpRemoteFile(fFile, ftpClient, currdir);
}
}else{
// 完全遍历
List<AFtpRemoteFile> rfileList = new ArrayList<AFtpRemoteFile>();
for (int i=0; names!=null&&i<names.length; i++) {
FTPFile[] rfile = ftpClient.listFiles(names[i]);
if (rfile!=null && rfile.length>0)
rfileList.add(new AFtpRemoteFile(rfile[0], ftpClient, currdir));
}
if (rfileList.size() <= 0){
ftpRemoteFiles = new AFtpRemoteFile[0];
}else{
ftpRemoteFiles = new AFtpRemoteFile[rfileList.size()];
rfileList.toArray(ftpRemoteFiles);
}
}
}
break;
}
case STORE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before store");
}else{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (ftpClient.storeFile(args[0], inputStream)){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case RETRIVE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before retrive");
}else{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (ftpClient.retrieveFile(args[0], outputStream)){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case RETRIVE_BY_STREAM: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before retriveByStream");
}else{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(args[0]);
}
break;
}
case STORE_BY_STREAM: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before storeByStream");
}else{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
outputStream = ftpClient.storeFileStream(args[0]);
}
break;
}
case COMPLETE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before completePendingCommand");
}else{
if(ftpClient.completePendingCommand()){
returnValue = "true";
}else{
returnValue = "false";
}
}
break;
}
case RENAME_FILE: {
if(ftpClient == null){
exception = new Exception("Please login with user and password before rename command");
}else{
if (ftpClient.rename(args[0], args[1])) {
returnValue = "true";
} else {
returnValue = "false";
}
}
break;
}
default:
break;
}
}catch(Exception e){
exception = e;
}finally{
doIt = false;
}
}
}
public void interrupt(){
run = false;
if(ftpClient != null){
try {
ftpClient.logout();
} catch (Exception e) {
}
try {
ftpClient.disconnect();
} catch (Exception e) {
}
try {
ftpClient.disconnect();
}catch(Exception e) {
}
}
super.interrupt();
}
public void doIt(int type, String[] args) {
this.type = type;
this.args = args;
returnValue = null;
ftpRemoteFiles = null;
ftpRemoteFile = null;
exception = null;
doIt = true;
}
public void doIt(int type, String[] args, InputStream inputStream) {
this.type = type;
this.args = args;
this.inputStream = inputStream;
returnValue = null;
ftpRemoteFiles = null;
ftpRemoteFile = null;
exception = null;
doIt = true;
}
public void doIt(int type, String[] args, OutputStream outputStream) {
this.type = type;
this.args = args;
this.outputStream = outputStream;
returnValue = null;
ftpRemoteFiles = null;
ftpRemoteFile = null;
exception = null;
doIt = true;
}
public boolean isDoIt() {
return doIt;
}
public String getReturnValue() {
return returnValue;
}
public Exception getException() {
return exception;
}
public boolean isPassiveMode() {
return passiveMode;
}
public void setPassiveMode(boolean passiveMode) {
this.passiveMode = passiveMode;
}
public AFtpRemoteFile[] getFtpRemoteFiles() {
return ftpRemoteFiles;
}
public AFtpRemoteFile getFtpRemoteFile() {
return ftpRemoteFile;
}
public InputStream getInputStream() {
return inputStream;
}
public OutputStream getOutputStream() {
return outputStream;
}
public FTPClient getFtpClient() {
return ftpClient;
}
/**
* @param args
*/
public static void main(String[] args) {
}
}