1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.libcheck;
18
19 import java.io.OutputStream;
20 import java.nio.charset.Charset;
21
22 import org.apache.commons.net.ftp.FTPClient;
23 import org.apache.commons.net.ftp.FTPFile;
24 import org.apache.commons.net.ftp.FTPReply;
25
26
27
28
29 public final class FtpCheck {
30
31 private static final int MAX_ARG_COUNT = 4;
32
33
34
35
36
37
38
39 public static void main(final String[] args) throws Exception {
40 if (args.length < 3) {
41 throw new IllegalArgumentException("Usage: FtpCheck user pass host dir");
42 }
43 final String user = args[0];
44 final String pass = args[1];
45 final String host = args[2];
46 String dir = null;
47 if (args.length == MAX_ARG_COUNT) {
48 dir = args[3];
49 }
50
51 final FTPClient client = new FTPClient();
52 client.connect(host);
53 final int reply = client.getReplyCode();
54 if (!FTPReply.isPositiveCompletion(reply)) {
55 throw new IllegalArgumentException("cant connect: " + reply);
56 }
57 if (!client.login(user, pass)) {
58 throw new IllegalArgumentException("login failed");
59 }
60 client.enterLocalPassiveMode();
61
62 final OutputStream os = client.storeFileStream(dir + "/test.txt");
63 if (os == null) {
64 throw new IllegalStateException(client.getReplyString());
65 }
66 os.write("test".getBytes(Charset.defaultCharset()));
67 os.close();
68 client.completePendingCommand();
69
70 if (dir != null && !client.changeWorkingDirectory(dir)) {
71 throw new IllegalArgumentException("change dir to '" + dir + "' failed");
72 }
73
74 System.err.println("System: " + client.getSystemType());
75
76 final FTPFile[] files = client.listFiles();
77 for (int i = 0; i < files.length; i++) {
78 final FTPFile file = files[i];
79 if (file == null) {
80 System.err.println("#" + i + ": " + null);
81 } else {
82 System.err.println("#" + i + ": " + file.getRawListing());
83 System.err.println("#" + i + ": " + file);
84 System.err.println("\t name:" + file.getName() + " type:" + file.getType());
85 }
86 }
87 client.disconnect();
88 }
89
90 private FtpCheck() {
91
92 }
93 }