001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package examples.ftp;
019
020import java.io.IOException;
021import java.io.PrintWriter;
022import java.net.InetAddress;
023
024import org.apache.commons.net.PrintCommandListener;
025import org.apache.commons.net.ProtocolCommandListener;
026import org.apache.commons.net.ftp.FTPClient;
027import org.apache.commons.net.ftp.FTPReply;
028
029/***
030 * This is an example program demonstrating how to use the FTPClient class.
031 * This program arranges a server to server file transfer that transfers
032 * a file from host1 to host2.  Keep in mind, this program might only work
033 * if host2 is the same as the host you run it on (for security reasons,
034 * some ftp servers only allow PORT commands to be issued with a host
035 * argument equal to the client host).
036 * <p>
037 * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>
038 ***/
039public final class ServerToServerFTP
040{
041
042    public static void main(String[] args)
043    {
044        String server1, username1, password1, file1;
045        String server2, username2, password2, file2;
046        String [] parts;
047        int port1=0, port2=0;
048        FTPClient ftp1, ftp2;
049        ProtocolCommandListener listener;
050
051        if (args.length < 8)
052        {
053            System.err.println(
054                "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>"
055            );
056            System.exit(1);
057        }
058
059        server1 = args[0];
060        parts = server1.split(":");
061        if (parts.length == 2) {
062            server1=parts[0];
063            port1 = Integer.parseInt(parts[1]);
064        }
065        username1 = args[1];
066        password1 = args[2];
067        file1 = args[3];
068        server2 = args[4];
069        parts = server2.split(":");
070        if (parts.length == 2) {
071            server2=parts[0];
072            port2 = Integer.parseInt(parts[1]);
073        }
074        username2 = args[5];
075        password2 = args[6];
076        file2 = args[7];
077
078        listener = new PrintCommandListener(new PrintWriter(System.out), true);
079        ftp1 = new FTPClient();
080        ftp1.addProtocolCommandListener(listener);
081        ftp2 = new FTPClient();
082        ftp2.addProtocolCommandListener(listener);
083
084        try
085        {
086            int reply;
087            if (port1 > 0) {
088                ftp1.connect(server1, port1);
089            } else {
090                ftp1.connect(server1);
091            }
092            System.out.println("Connected to " + server1 + ".");
093
094            reply = ftp1.getReplyCode();
095
096            if (!FTPReply.isPositiveCompletion(reply))
097            {
098                ftp1.disconnect();
099                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}