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