1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.examples.ftp;
19
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.net.InetAddress;
23
24 import org.apache.commons.net.PrintCommandListener;
25 import org.apache.commons.net.ProtocolCommandListener;
26 import org.apache.commons.net.ftp.FTPClient;
27 import org.apache.commons.net.ftp.FTPReply;
28
29
30
31
32
33
34
35
36
37
38
39 public final class ServerToServerFTP
40 {
41
42 public static void main(final String[] args)
43 {
44 String server1;
45 final String username1;
46 final String password1;
47 final String file1;
48 String server2;
49 final String username2;
50 final String password2;
51 final String file2;
52 String [] parts;
53 int port1=0, port2=0;
54 final FTPClient ftp1;
55 final FTPClient ftp2;
56 final ProtocolCommandListener listener;
57
58 if (args.length < 8)
59 {
60 System.err.println(
61 "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>"
62 );
63 System.exit(1);
64 }
65
66 server1 = args[0];
67 parts = server1.split(":");
68 if (parts.length == 2) {
69 server1=parts[0];
70 port1 = Integer.parseInt(parts[1]);
71 }
72 username1 = args[1];
73 password1 = args[2];
74 file1 = args[3];
75 server2 = args[4];
76 parts = server2.split(":");
77 if (parts.length == 2) {
78 server2=parts[0];
79 port2 = Integer.parseInt(parts[1]);
80 }
81 username2 = args[5];
82 password2 = args[6];
83 file2 = args[7];
84
85 listener = new PrintCommandListener(new PrintWriter(System.out), true);
86 ftp1 = new FTPClient();
87 ftp1.addProtocolCommandListener(listener);
88 ftp2 = new FTPClient();
89 ftp2.addProtocolCommandListener(listener);
90
91 try
92 {
93 final int reply;
94 if (port1 > 0) {
95 ftp1.connect(server1, port1);
96 } else {
97 ftp1.connect(server1);
98 }
99 System.out.println("Connected to " + server1 + ".");
100
101 reply = ftp1.getReplyCode();
102
103 if (!FTPReply.isPositiveCompletion(reply))
104 {
105 ftp1.disconnect();
106 System.err.println("FTP server1 refused connection.");
107 System.exit(1);
108 }
109 }
110 catch (final IOException e)
111 {
112 if (ftp1.isConnected())
113 {
114 try
115 {
116 ftp1.disconnect();
117 }
118 catch (final IOException f)
119 {
120
121 }
122 }
123 System.err.println("Could not connect to server1.");
124 e.printStackTrace();
125 System.exit(1);
126 }
127
128 try
129 {
130 final int reply;
131 if (port2 > 0) {
132 ftp2.connect(server2, port2);
133 } else {
134 ftp2.connect(server2);
135 }
136 System.out.println("Connected to " + server2 + ".");
137
138 reply = ftp2.getReplyCode();
139
140 if (!FTPReply.isPositiveCompletion(reply))
141 {
142 ftp2.disconnect();
143 System.err.println("FTP server2 refused connection.");
144 System.exit(1);
145 }
146 }
147 catch (final IOException e)
148 {
149 if (ftp2.isConnected())
150 {
151 try
152 {
153 ftp2.disconnect();
154 }
155 catch (final IOException f)
156 {
157
158 }
159 }
160 System.err.println("Could not connect to server2.");
161 e.printStackTrace();
162 System.exit(1);
163 }
164
165 __main:
166 try
167 {
168 if (!ftp1.login(username1, password1))
169 {
170 System.err.println("Could not login to " + server1);
171 break __main;
172 }
173
174 if (!ftp2.login(username2, password2))
175 {
176 System.err.println("Could not login to " + server2);
177 break __main;
178 }
179
180
181 ftp2.enterRemotePassiveMode();
182
183 ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()),
184 ftp2.getPassivePort());
185
186
187
188
189
190
191
192
193 if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2))
194 {
195
196
197 ftp1.completePendingCommand();
198 ftp2.completePendingCommand();
199 }
200 else
201 {
202 System.err.println(
203 "Couldn't initiate transfer. Check that file names are valid.");
204 break __main;
205 }
206
207 }
208 catch (final IOException e)
209 {
210 e.printStackTrace();
211 System.exit(1);
212 }
213 finally
214 {
215 try
216 {
217 if (ftp1.isConnected())
218 {
219 ftp1.logout();
220 ftp1.disconnect();
221 }
222 }
223 catch (final IOException e)
224 {
225
226 }
227
228 try
229 {
230 if (ftp2.isConnected())
231 {
232 ftp2.logout();
233 ftp2.disconnect();
234 }
235 }
236 catch (final IOException e)
237 {
238
239 }
240 }
241 }
242 }