001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package examples;
017    
018    import java.io.FileInputStream;
019    import java.io.FileOutputStream;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.OutputStream;
023    import java.io.PrintWriter;
024    import org.apache.commons.net.ftp.FTP;
025    import org.apache.commons.net.ftp.FTPClient;
026    import org.apache.commons.net.ftp.FTPConnectionClosedException;
027    import org.apache.commons.net.ftp.FTPReply;
028    
029    /***
030     * This is an example program demonstrating how to use the FTPClient class.
031     * This program connects to an FTP server and retrieves the specified
032     * file.  If the -s flag is used, it stores the local file at the FTP server.
033     * Just so you can see what's happening, all reply strings are printed.
034     * If the -b flag is used, a binary transfer is assumed (default is ASCII).
035     * <p>
036     * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
037     * <p>
038     ***/
039    public final class ftp
040    {
041    
042        public static final String USAGE =
043            "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
044            "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
045            "\t-s store file on server (upload)\n" +
046            "\t-b use binary transfer mode\n";
047    
048        public static final void main(String[] args)
049        {
050            int base = 0;
051            boolean storeFile = false, binaryTransfer = false, error = false;
052            String server, username, password, remote, local;
053            FTPClient ftp;
054    
055            for (base = 0; base < args.length; base++)
056            {
057                if (args[base].startsWith("-s"))
058                    storeFile = true;
059                else if (args[base].startsWith("-b"))
060                    binaryTransfer = true;
061                else
062                    break;
063            }
064    
065            if ((args.length - base) != 5)
066            {
067                System.err.println(USAGE);
068                System.exit(1);
069            }
070    
071            server = args[base++];
072            username = args[base++];
073            password = args[base++];
074            remote = args[base++];
075            local = args[base];
076    
077            ftp = new FTPClient();
078            ftp.addProtocolCommandListener(new PrintCommandListener(
079                                               new PrintWriter(System.out)));
080    
081            try
082            {
083                int reply;
084                ftp.connect(server);
085                System.out.println("Connected to " + server + ".");
086    
087                // After connection attempt, you should check the reply code to verify
088                // success.
089                reply = ftp.getReplyCode();
090    
091                if (!FTPReply.isPositiveCompletion(reply))
092                {
093                    ftp.disconnect();
094                    System.err.println("FTP server refused connection.");
095                    System.exit(1);
096                }
097            }
098            catch (IOException e)
099            {
100                if (ftp.isConnected())
101                {
102                    try
103                    {
104                        ftp.disconnect();
105                    }
106                    catch (IOException f)
107                    {
108                        // do nothing
109                    }
110                }
111                System.err.println("Could not connect to server.");
112                e.printStackTrace();
113                System.exit(1);
114            }
115    
116    __main:
117            try
118            {
119                if (!ftp.login(username, password))
120                {
121                    ftp.logout();
122                    error = true;
123                    break __main;
124                }
125    
126                System.out.println("Remote system is " + ftp.getSystemName());
127    
128                if (binaryTransfer)
129                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
130    
131                // Use passive mode as default because most of us are
132                // behind firewalls these days.
133                ftp.enterLocalPassiveMode();
134    
135                if (storeFile)
136                {
137                    InputStream input;
138    
139                    input = new FileInputStream(local);
140    
141                    ftp.storeFile(remote, input);
142    
143                    input.close();
144                }
145                else
146                {
147                    OutputStream output;
148    
149                    output = new FileOutputStream(local);
150    
151                    ftp.retrieveFile(remote, output);
152    
153                    output.close();
154                }
155    
156                ftp.logout();
157            }
158            catch (FTPConnectionClosedException e)
159            {
160                error = true;
161                System.err.println("Server closed connection.");
162                e.printStackTrace();
163            }
164            catch (IOException e)
165            {
166                error = true;
167                e.printStackTrace();
168            }
169            finally
170            {
171                if (ftp.isConnected())
172                {
173                    try
174                    {
175                        ftp.disconnect();
176                    }
177                    catch (IOException f)
178                    {
179                        // do nothing
180                    }
181                }
182            }
183    
184            System.exit(error ? 1 : 0);
185        } // end main
186    
187    }
188