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