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