1 package examples;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.IOException;
58 import java.io.PrintWriter;
59 import java.net.InetAddress;
60 import org.apache.commons.net.ProtocolCommandListener;
61 import org.apache.commons.net.ftp.FTPClient;
62 import org.apache.commons.net.ftp.FTPReply;
63
64 /****
65 * This is an example program demonstrating how to use the FTPClient class.
66 * This program arranges a server to server file transfer that transfers
67 * a file from host1 to host2. Keep in mind, this program might only work
68 * if host2 is the same as the host you run it on (for security reasons,
69 * some ftp servers only allow PORT commands to be issued with a host
70 * argument equal to the client host).
71 * <p>
72 * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>
73 * <p>
74 ***/
75 public final class server2serverFTP
76 {
77
78 public static final void main(String[] args)
79 {
80 String server1, username1, password1, file1;
81 String server2, username2, password2, file2;
82 FTPClient ftp1, ftp2;
83 ProtocolCommandListener listener;
84
85 if (args.length < 8)
86 {
87 System.err.println(
88 "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>"
89 );
90 System.exit(1);
91 }
92
93 server1 = args[0];
94 username1 = args[1];
95 password1 = args[2];
96 file1 = args[3];
97 server2 = args[4];
98 username2 = args[5];
99 password2 = args[6];
100 file2 = args[7];
101
102 listener = new PrintCommandListener(new PrintWriter(System.out));
103 ftp1 = new FTPClient();
104 ftp1.addProtocolCommandListener(listener);
105 ftp2 = new FTPClient();
106 ftp2.addProtocolCommandListener(listener);
107
108 try
109 {
110 int reply;
111 ftp1.connect(server1);
112 System.out.println("Connected to " + server1 + ".");
113
114 reply = ftp1.getReplyCode();
115
116 if (!FTPReply.isPositiveCompletion(reply))
117 {
118 ftp1.disconnect();
119 System.err.println("FTP server1 refused connection.");
120 System.exit(1);
121 }
122 }
123 catch (IOException e)
124 {
125 if (ftp1.isConnected())
126 {
127 try
128 {
129 ftp1.disconnect();
130 }
131 catch (IOException f)
132 {
133 // do nothing
134 }
135 }
136 System.err.println("Could not connect to server1.");
137 e.printStackTrace();
138 System.exit(1);
139 }
140
141 try
142 {
143 int reply;
144 ftp2.connect(server2);
145 System.out.println("Connected to " + server2 + ".");
146
147 reply = ftp2.getReplyCode();
148
149 if (!FTPReply.isPositiveCompletion(reply))
150 {
151 ftp2.disconnect();
152 System.err.println("FTP server2 refused connection.");
153 System.exit(1);
154 }
155 }
156 catch (IOException e)
157 {
158 if (ftp2.isConnected())
159 {
160 try
161 {
162 ftp2.disconnect();
163 }
164 catch (IOException f)
165 {
166 // do nothing
167 }
168 }
169 System.err.println("Could not connect to server2.");
170 e.printStackTrace();
171 System.exit(1);
172 }
173
174 __main:
175 try
176 {
177 if (!ftp1.login(username1, password1))
178 {
179 System.err.println("Could not login to " + server1);
180 break __main;
181 }
182
183 if (!ftp2.login(username2, password2))
184 {
185 System.err.println("Could not login to " + server2);
186 break __main;
187 }
188
189 // Let's just assume success for now.
190 ftp2.enterRemotePassiveMode();
191
192 ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()),
193 ftp2.getPassivePort());
194
195 // Although you would think the store command should be sent to server2
196 // first, in reality, ftp servers like wu-ftpd start accepting data
197 // connections right after entering passive mode. Additionally, they
198 // don't even send the positive preliminary reply until after the
199 // transfer is completed (in the case of passive mode transfers).
200 // Therefore, calling store first would hang waiting for a preliminary
201 // reply.
202 if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2))
203 {
204 // if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
205 // We have to fetch the positive completion reply.
206 ftp1.completePendingCommand();
207 ftp2.completePendingCommand();
208 }
209 else
210 {
211 System.err.println(
212 "Couldn't initiate transfer. Check that filenames are valid.");
213 break __main;
214 }
215
216 }
217 catch (IOException e)
218 {
219 e.printStackTrace();
220 System.exit(1);
221 }
222 finally
223 {
224 try
225 {
226 if (ftp1.isConnected())
227 {
228 ftp1.logout();
229 ftp1.disconnect();
230 }
231 }
232 catch (IOException e)
233 {
234 // do nothing
235 }
236
237 try
238 {
239 if (ftp2.isConnected())
240 {
241 ftp2.logout();
242 ftp2.disconnect();
243 }
244 }
245 catch (IOException e)
246 {
247 // do nothing
248 }
249 }
250 }
251 }
This page was automatically generated by Maven