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 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(String[] args)
43      {
44          String server1, username1, password1, file1;
45          String server2, username2, password2, file2;
46          String [] parts;
47          int port1=0, port2=0;
48          FTPClient ftp1, ftp2;
49          ProtocolCommandListener listener;
50  
51          if (args.length < 8)
52          {
53              System.err.println(
54                  "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>"
55              );
56              System.exit(1);
57          }
58  
59          server1 = args[0];
60          parts = server1.split(":");
61          if (parts.length == 2) {
62              server1=parts[0];
63              port1 = Integer.parseInt(parts[1]);
64          }
65          username1 = args[1];
66          password1 = args[2];
67          file1 = args[3];
68          server2 = args[4];
69          parts = server2.split(":");
70          if (parts.length == 2) {
71              server2=parts[0];
72              port2 = Integer.parseInt(parts[1]);
73          }
74          username2 = args[5];
75          password2 = args[6];
76          file2 = args[7];
77  
78          listener = new PrintCommandListener(new PrintWriter(System.out), true);
79          ftp1 = new FTPClient();
80          ftp1.addProtocolCommandListener(listener);
81          ftp2 = new FTPClient();
82          ftp2.addProtocolCommandListener(listener);
83  
84          try
85          {
86              int reply;
87              if (port1 > 0) {
88                  ftp1.connect(server1, port1);
89              } else {
90                  ftp1.connect(server1);
91              }
92              System.out.println("Connected to " + server1 + ".");
93  
94              reply = ftp1.getReplyCode();
95  
96              if (!FTPReply.isPositiveCompletion(reply))
97              {
98                  ftp1.disconnect();
99                  System.err.println("FTP server1 refused connection.");
100                 System.exit(1);
101             }
102         }
103         catch (IOException e)
104         {
105             if (ftp1.isConnected())
106             {
107                 try
108                 {
109                     ftp1.disconnect();
110                 }
111                 catch (IOException f)
112                 {
113                     // do nothing
114                 }
115             }
116             System.err.println("Could not connect to server1.");
117             e.printStackTrace();
118             System.exit(1);
119         }
120 
121         try
122         {
123             int reply;
124             if (port2 > 0) {
125                 ftp2.connect(server2, port2);
126             } else {
127                 ftp2.connect(server2);
128             }
129             System.out.println("Connected to " + server2 + ".");
130 
131             reply = ftp2.getReplyCode();
132 
133             if (!FTPReply.isPositiveCompletion(reply))
134             {
135                 ftp2.disconnect();
136                 System.err.println("FTP server2 refused connection.");
137                 System.exit(1);
138             }
139         }
140         catch (IOException e)
141         {
142             if (ftp2.isConnected())
143             {
144                 try
145                 {
146                     ftp2.disconnect();
147                 }
148                 catch (IOException f)
149                 {
150                     // do nothing
151                 }
152             }
153             System.err.println("Could not connect to server2.");
154             e.printStackTrace();
155             System.exit(1);
156         }
157 
158 __main:
159         try
160         {
161             if (!ftp1.login(username1, password1))
162             {
163                 System.err.println("Could not login to " + server1);
164                 break __main;
165             }
166 
167             if (!ftp2.login(username2, password2))
168             {
169                 System.err.println("Could not login to " + server2);
170                 break __main;
171             }
172 
173             // Let's just assume success for now.
174             ftp2.enterRemotePassiveMode();
175 
176             ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()),
177                                        ftp2.getPassivePort());
178 
179             // Although you would think the store command should be sent to server2
180             // first, in reality, ftp servers like wu-ftpd start accepting data
181             // connections right after entering passive mode.  Additionally, they
182             // don't even send the positive preliminary reply until after the
183             // transfer is completed (in the case of passive mode transfers).
184             // Therefore, calling store first would hang waiting for a preliminary
185             // reply.
186             if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2))
187             {
188                 //      if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
189                 // We have to fetch the positive completion reply.
190                 ftp1.completePendingCommand();
191                 ftp2.completePendingCommand();
192             }
193             else
194             {
195                 System.err.println(
196                     "Couldn't initiate transfer.  Check that filenames are valid.");
197                 break __main;
198             }
199 
200         }
201         catch (IOException e)
202         {
203             e.printStackTrace();
204             System.exit(1);
205         }
206         finally
207         {
208             try
209             {
210                 if (ftp1.isConnected())
211                 {
212                     ftp1.logout();
213                     ftp1.disconnect();
214                 }
215             }
216             catch (IOException e)
217             {
218                 // do nothing
219             }
220 
221             try
222             {
223                 if (ftp2.isConnected())
224                 {
225                     ftp2.logout();
226                     ftp2.disconnect();
227                 }
228             }
229             catch (IOException e)
230             {
231                 // do nothing
232             }
233         }
234     }
235 }