    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.File;
    58  import java.io.FileInputStream;
    59  import java.io.FileOutputStream;
    60  import java.io.IOException;
    61  import java.net.SocketException;
    62  import java.net.UnknownHostException;
    63  import org.apache.commons.net.tftp.TFTP;
    64  import org.apache.commons.net.tftp.TFTPClient;
    65
    66  /****
    67   * This is an example of a simple Java tftp client using NetComponents.
    68   * Notice how all of the code is really just argument processing and
    69   * error handling.
    70   * <p>
    71   * Usage: tftp [options] hostname localfile remotefile
    72   * hostname   - The name of the remote host
    73   * localfile  - The name of the local file to send or the name to use for
    74   *              the received file
    75   * remotefile - The name of the remote file to receive or the name for
    76   *              the remote server to use to name the local file being sent.
    77   * options: (The default is to assume -r -b)
    78   *        -s Send a local file
    79   *        -r Receive a remote file
    80   *        -a Use ASCII transfer mode
    81   *        -b Use binary transfer mode
    82   * <p>
    83   ***/
    84  public final class tftp
    85  {
    86      static final String USAGE =
    87          "Usage: tftp [options] hostname localfile remotefile\n\n" +
    88          "hostname   - The name of the remote host\n" +
    89          "localfile  - The name of the local file to send or the name to use for\n" +
    90          "\tthe received file\n" +
    91          "remotefile - The name of the remote file to receive or the name for\n" +
    92          "\tthe remote server to use to name the local file being sent.\n\n" +
    93          "options: (The default is to assume -r -b)\n" +
    94          "\t-s Send a local file\n" +
    95          "\t-r Receive a remote file\n" +
    96          "\t-a Use ASCII transfer mode\n" +
    97          "\t-b Use binary transfer mode\n";
    98
    99      public final static void main(String[] args)
    100     {
    101         boolean receiveFile = true, closed;
    102         int transferMode = TFTP.BINARY_MODE, argc;
    103         String arg, hostname, localFilename, remoteFilename;
    104         TFTPClient tftp;
    105
    106         // Parse options
    107         for (argc = 0; argc < args.length; argc++)
    108         {
    109             arg = args[argc];
    110             if (arg.startsWith("-"))
    111             {
    112                 if (arg.equals("-r"))
    113                     receiveFile = true;
    114                 else if (arg.equals("-s"))
    115                     receiveFile = false;
    116                 else if (arg.equals("-a"))
    117                     transferMode = TFTP.ASCII_MODE;
    118                 else if (arg.equals("-b"))
    119                     transferMode = TFTP.BINARY_MODE;
    120                 else
    121                 {
    122                     System.err.println("Error: unrecognized option.");
    123                     System.err.print(USAGE);
    124                     System.exit(1);
    125                 }
    126             }
    127             else
    128                 break;
    129         }
    130
    131         // Make sure there are enough arguments
    132         if (args.length - argc != 3)
    133         {
    134             System.err.println("Error: invalid number of arguments.");
    135             System.err.print(USAGE);
    136             System.exit(1);
    137         }
    138
    139         // Get host and file arguments
    140         hostname = args[argc];
    141         localFilename = args[argc + 1];
    142         remoteFilename = args[argc + 2];
    143
    144         // Create our TFTP instance to handle the file transfer.
    145         tftp = new TFTPClient();
    146
    147         // We want to timeout if a response takes longer than 60 seconds
    148         tftp.setDefaultTimeout(60000);
    149
    150         // Open local socket
    151         try
    152         {
    153             tftp.open();
    154         }
    155         catch (SocketException e)
    156         {
    157             System.err.println("Error: could not open local UDP socket.");
    158             System.err.println(e.getMessage());
    159             System.exit(1);
    160         }
    161
    162         // We haven't closed the local file yet.
    163         closed = false;
    164
    165         // If we're receiving a file, receive, otherwise send.
    166         if (receiveFile)
    167         {
    168             FileOutputStream output = null;
    169             File file;
    170
    171             file = new File(localFilename);
    172
    173             // If file exists, don't overwrite it.
    174             if (file.exists())
    175             {
    176                 System.err.println("Error: " + localFilename + " already exists.");
    177                 System.exit(1);
    178             }
    179
    180             // Try to open local file for writing
    181             try
    182             {
    183                 output = new FileOutputStream(file);
    184             }
    185             catch (IOException e)
    186             {
    187                 tftp.close();
    188                 System.err.println("Error: could not open local file for writing.");
    189                 System.err.println(e.getMessage());
    190                 System.exit(1);
    191             }
    192
    193             // Try to receive remote file via TFTP
    194             try
    195             {
    196                 tftp.receiveFile(remoteFilename, transferMode, output, hostname);
    197             }
    198             catch (UnknownHostException e)
    199             {
    200                 System.err.println("Error: could not resolve hostname.");
    201                 System.err.println(e.getMessage());
    202                 System.exit(1);
    203             }
    204             catch (IOException e)
    205             {
    206                 System.err.println(
    207                     "Error: I/O exception occurred while receiving file.");
    208                 System.err.println(e.getMessage());
    209                 System.exit(1);
    210             }
    211             finally
    212             {
    213                 // Close local socket and output file
    214                 tftp.close();
    215                 try
    216                 {
    217                     output.close();
    218                     closed = true;
    219                 }
    220                 catch (IOException e)
    221                 {
    222                     closed = false;
    223                     System.err.println("Error: error closing file.");
    224                     System.err.println(e.getMessage());
    225                 }
    226             }
    227
    228             if (!closed)
    229                 System.exit(1);
    230
    231         }
    232         else
    233         {
    234             // We're sending a file
    235             FileInputStream input = null;
    236
    237             // Try to open local file for reading
    238             try
    239             {
    240                 input = new FileInputStream(localFilename);
    241             }
    242             catch (IOException e)
    243             {
    244                 tftp.close();
    245                 System.err.println("Error: could not open local file for reading.");
    246                 System.err.println(e.getMessage());
    247                 System.exit(1);
    248             }
    249
    250             // Try to send local file via TFTP
    251             try
    252             {
    253                 tftp.sendFile(remoteFilename, transferMode, input, hostname);
    254             }
    255             catch (UnknownHostException e)
    256             {
    257                 System.err.println("Error: could not resolve hostname.");
    258                 System.err.println(e.getMessage());
    259                 System.exit(1);
    260             }
    261             catch (IOException e)
    262             {
    263                 System.err.println(
    264                     "Error: I/O exception occurred while sending file.");
    265                 System.err.println(e.getMessage());
    266                 System.exit(1);
    267             }
    268             finally
    269             {
    270                 // Close local socket and input file
    271                 tftp.close();
    272                 try
    273                 {
    274                     input.close();
    275                     closed = true;
    276                 }
    277                 catch (IOException e)
    278                 {
    279                     closed = false;
    280                     System.err.println("Error: error closing file.");
    281                     System.err.println(e.getMessage());
    282                 }
    283             }
    284
    285             if (!closed)
    286                 System.exit(1);
    287
    288         }
    289
    290     }
    291
    292 }
    293
    294

This page was automatically generated by Maven