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