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 private FtpCheck() {
31
32 }
33
34 public static void main(final String[] args) throws Exception {
35 if (args.length < 3) {
36 throw new IllegalArgumentException("Usage: FtpCheck user pass host dir");
37 }
38 final String user = args[0];
39 final String pass = args[1];
40 final String host = args[2];
41 String dir = null;
42 if (args.length == 4) {
43 dir = args[3];
44 }
45
46 final FTPClient client = new FTPClient();
47 client.connect(host);
48 final int reply = client.getReplyCode();
49 if (!FTPReply.isPositiveCompletion(reply)) {
50 throw new IllegalArgumentException("cant connect: " + reply);
51 }
52 if (!client.login(user, pass)) {
53 throw new IllegalArgumentException("login failed");
54 }
55 client.enterLocalPassiveMode();
56
57 final OutputStream os = client.storeFileStream(dir + "/test.txt");
58 if (os == null) {
59 throw new IllegalStateException(client.getReplyString());
60 }
61 os.write("test".getBytes(Charset.defaultCharset()));
62 os.close();
63 client.completePendingCommand();
64
65 if (dir != null && !client.changeWorkingDirectory(dir)) {
66 throw new IllegalArgumentException("change dir to '" + dir + "' failed");
67 }
68
69 System.err.println("System: " + client.getSystemType());
70
71 final FTPFile[] files = client.listFiles();
72 for (int i = 0; i < files.length; i++) {
73 final FTPFile file = files[i];
74 if (file == null) {
75 System.err.println("#" + i + ": " + null);
76 } else {
77 System.err.println("#" + i + ": " + file.getRawListing());
78 System.err.println("#" + i + ": " + file.toString());
79 System.err.println("\t name:" + file.getName() + " type:" + file.getType());
80 }
81 }
82 client.disconnect();
83 }
84 }