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