1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package examples;
19
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.PrintWriter;
26
27 import org.apache.commons.net.PrintCommandListener;
28 import org.apache.commons.net.ftp.FTP;
29 import org.apache.commons.net.ftp.FTPClient;
30 import org.apache.commons.net.ftp.FTPConnectionClosedException;
31 import org.apache.commons.net.ftp.FTPReply;
32
33
34
35
36
37
38
39
40
41
42
43 public final class FTPExample
44 {
45
46 public static final String USAGE =
47 "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
48 "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
49 "\t-s store file on server (upload)\n" +
50 "\t-b use binary transfer mode\n";
51
52 public static final void main(String[] args)
53 {
54 int base = 0;
55 boolean storeFile = false, binaryTransfer = false, error = false;
56 String server, username, password, remote, local;
57 FTPClient ftp;
58
59 for (base = 0; base < args.length; base++)
60 {
61 if (args[base].startsWith("-s"))
62 storeFile = true;
63 else if (args[base].startsWith("-b"))
64 binaryTransfer = true;
65 else
66 break;
67 }
68
69 if ((args.length - base) != 5)
70 {
71 System.err.println(USAGE);
72 System.exit(1);
73 }
74
75 server = args[base++];
76 username = args[base++];
77 password = args[base++];
78 remote = args[base++];
79 local = args[base];
80
81 ftp = new FTPClient();
82 ftp.addProtocolCommandListener(new PrintCommandListener(
83 new PrintWriter(System.out)));
84
85 try
86 {
87 int reply;
88 ftp.connect(server);
89 System.out.println("Connected to " + server + ".");
90
91
92
93 reply = ftp.getReplyCode();
94
95 if (!FTPReply.isPositiveCompletion(reply))
96 {
97 ftp.disconnect();
98 System.err.println("FTP server refused connection.");
99 System.exit(1);
100 }
101 }
102 catch (IOException e)
103 {
104 if (ftp.isConnected())
105 {
106 try
107 {
108 ftp.disconnect();
109 }
110 catch (IOException f)
111 {
112
113 }
114 }
115 System.err.println("Could not connect to server.");
116 e.printStackTrace();
117 System.exit(1);
118 }
119
120 __main:
121 try
122 {
123 if (!ftp.login(username, password))
124 {
125 ftp.logout();
126 error = true;
127 break __main;
128 }
129
130 System.out.println("Remote system is " + ftp.getSystemName());
131
132 if (binaryTransfer)
133 ftp.setFileType(FTP.BINARY_FILE_TYPE);
134
135
136
137 ftp.enterLocalPassiveMode();
138
139 if (storeFile)
140 {
141 InputStream input;
142
143 input = new FileInputStream(local);
144
145 ftp.storeFile(remote, input);
146
147 input.close();
148 }
149 else
150 {
151 OutputStream output;
152
153 output = new FileOutputStream(local);
154
155 ftp.retrieveFile(remote, output);
156
157 output.close();
158 }
159
160 ftp.logout();
161 }
162 catch (FTPConnectionClosedException e)
163 {
164 error = true;
165 System.err.println("Server closed connection.");
166 e.printStackTrace();
167 }
168 catch (IOException e)
169 {
170 error = true;
171 e.printStackTrace();
172 }
173 finally
174 {
175 if (ftp.isConnected())
176 {
177 try
178 {
179 ftp.disconnect();
180 }
181 catch (IOException f)
182 {
183
184 }
185 }
186 }
187
188 System.exit(error ? 1 : 0);
189 }
190
191 }
192