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