    View Javadoc
    1   package examples;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.io.FileInputStream;
    58  import java.io.FileOutputStream;
    59  import java.io.IOException;
    60  import java.io.InputStream;
    61  import java.io.OutputStream;
    62  import java.io.PrintWriter;
    63  import org.apache.commons.net.ftp.FTP;
    64  import org.apache.commons.net.ftp.FTPClient;
    65  import org.apache.commons.net.ftp.FTPConnectionClosedException;
    66  import org.apache.commons.net.ftp.FTPReply;
    67
    68  /****
    69   * This is an example program demonstrating how to use the FTPClient class.
    70   * This program connects to an FTP server and retrieves the specified
    71   * file.  If the -s flag is used, it stores the local file at the FTP server.
    72   * Just so you can see what's happening, all reply strings are printed.
    73   * If the -b flag is used, a binary transfer is assumed (default is ASCII).
    74   * <p>
    75   * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
    76   * <p>
    77   ***/
    78  public final class ftp
    79  {
    80
    81      public static final String USAGE =
    82          "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
    83          "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
    84          "\t-s store file on server (upload)\n" +
    85          "\t-b use binary transfer mode\n";
    86
    87      public static final void main(String[] args)
    88      {
    89          int base = 0;
    90          boolean storeFile = false, binaryTransfer = false, error = false;
    91          String server, username, password, remote, local;
    92          FTPClient ftp;
    93
    94          for (base = 0; base < args.length; base++)
    95          {
    96              if (args[base].startsWith("-s"))
    97                  storeFile = true;
    98              else if (args[base].startsWith("-b"))
    99                  binaryTransfer = true;
    100             else
    101                 break;
    102         }
    103
    104         if ((args.length - base) != 5)
    105         {
    106             System.err.println(USAGE);
    107             System.exit(1);
    108         }
    109
    110         server = args[base++];
    111         username = args[base++];
    112         password = args[base++];
    113         remote = args[base++];
    114         local = args[base];
    115
    116         ftp = new FTPClient();
    117         ftp.addProtocolCommandListener(new PrintCommandListener(
    118                                            new PrintWriter(System.out)));
    119
    120         try
    121         {
    122             int reply;
    123             ftp.connect(server);
    124             System.out.println("Connected to " + server + ".");
    125
    126             // After connection attempt, you should check the reply code to verify
    127             // success.
    128             reply = ftp.getReplyCode();
    129
    130             if (!FTPReply.isPositiveCompletion(reply))
    131             {
    132                 ftp.disconnect();
    133                 System.err.println("FTP server refused connection.");
    134                 System.exit(1);
    135             }
    136         }
    137         catch (IOException e)
    138         {
    139             if (ftp.isConnected())
    140             {
    141                 try
    142                 {
    143                     ftp.disconnect();
    144                 }
    145                 catch (IOException f)
    146                 {
    147                     // do nothing
    148                 }
    149             }
    150             System.err.println("Could not connect to server.");
    151             e.printStackTrace();
    152             System.exit(1);
    153         }
    154
    155 __main:
    156         try
    157         {
    158             if (!ftp.login(username, password))
    159             {
    160                 ftp.logout();
    161                 error = true;
    162                 break __main;
    163             }
    164
    165             System.out.println("Remote system is " + ftp.getSystemName());
    166
    167             if (binaryTransfer)
    168                 ftp.setFileType(FTP.BINARY_FILE_TYPE);
    169
    170 	    // Use passive mode as default because most of us are
    171 	    // behind firewalls these days.
    172 	    ftp.enterLocalPassiveMode();
    173
    174             if (storeFile)
    175             {
    176                 InputStream input;
    177
    178                 input = new FileInputStream(local);
    179                 ftp.storeFile(remote, input);
    180             }
    181             else
    182             {
    183                 OutputStream output;
    184
    185                 output = new FileOutputStream(local);
    186                 ftp.retrieveFile(remote, output);
    187             }
    188
    189             ftp.logout();
    190         }
    191         catch (FTPConnectionClosedException e)
    192         {
    193             error = true;
    194             System.err.println("Server closed connection.");
    195             e.printStackTrace();
    196         }
    197         catch (IOException e)
    198         {
    199             error = true;
    200             e.printStackTrace();
    201         }
    202         finally
    203         {
    204             if (ftp.isConnected())
    205             {
    206                 try
    207                 {
    208                     ftp.disconnect();
    209                 }
    210                 catch (IOException f)
    211                 {
    212                     // do nothing
    213                 }
    214             }
    215         }
    216
    217         System.exit(error ? 1 : 0);
    218     } // end main
    219
    220 }
    221

This page was automatically generated by Maven