View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * This is an example program demonstrating how to use the FTPClient class.
31   * This program arranges a server to server file transfer that transfers
32   * a file from host1 to host2.  Keep in mind, this program might only work
33   * if host2 is the same as the host you run it on (for security reasons,
34   * some ftp servers only allow PORT commands to be issued with a host
35   * argument equal to the client host).
36   * <p>
37   * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>
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                     // do nothing
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                     // do nothing
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             // Let's just assume success for now.
181             ftp2.enterRemotePassiveMode();
182 
183             ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()),
184                                        ftp2.getPassivePort());
185 
186             // Although you would think the store command should be sent to server2
187             // first, in reality, ftp servers like wu-ftpd start accepting data
188             // connections right after entering passive mode.  Additionally, they
189             // don't even send the positive preliminary reply until after the
190             // transfer is completed (in the case of passive mode transfers).
191             // Therefore, calling store first would hang waiting for a preliminary
192             // reply.
193             if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2))
194             {
195                 //      if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
196                 // We have to fetch the positive completion reply.
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                 // do nothing
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                 // do nothing
239             }
240         }
241     }
242 }