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