View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package examples;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.net.SocketException;
23  import java.net.UnknownHostException;
24  import org.apache.commons.net.tftp.TFTP;
25  import org.apache.commons.net.tftp.TFTPClient;
26  
27  /***
28   * This is an example of a simple Java tftp client using NetComponents.
29   * Notice how all of the code is really just argument processing and
30   * error handling.
31   * <p>
32   * Usage: tftp [options] hostname localfile remotefile
33   * hostname   - The name of the remote host
34   * localfile  - The name of the local file to send or the name to use for
35   *              the received file
36   * remotefile - The name of the remote file to receive or the name for
37   *              the remote server to use to name the local file being sent.
38   * options: (The default is to assume -r -b)
39   *        -s Send a local file
40   *        -r Receive a remote file
41   *        -a Use ASCII transfer mode
42   *        -b Use binary transfer mode
43   * <p>
44   ***/
45  public final class tftp
46  {
47      static final String USAGE =
48          "Usage: tftp [options] hostname localfile remotefile\n\n" +
49          "hostname   - The name of the remote host\n" +
50          "localfile  - The name of the local file to send or the name to use for\n" +
51          "\tthe received file\n" +
52          "remotefile - The name of the remote file to receive or the name for\n" +
53          "\tthe remote server to use to name the local file being sent.\n\n" +
54          "options: (The default is to assume -r -b)\n" +
55          "\t-s Send a local file\n" +
56          "\t-r Receive a remote file\n" +
57          "\t-a Use ASCII transfer mode\n" +
58          "\t-b Use binary transfer mode\n";
59  
60      public final static void main(String[] args)
61      {
62          boolean receiveFile = true, closed;
63          int transferMode = TFTP.BINARY_MODE, argc;
64          String arg, hostname, localFilename, remoteFilename;
65          TFTPClient tftp;
66  
67          // Parse options
68          for (argc = 0; argc < args.length; argc++)
69          {
70              arg = args[argc];
71              if (arg.startsWith("-"))
72              {
73                  if (arg.equals("-r"))
74                      receiveFile = true;
75                  else if (arg.equals("-s"))
76                      receiveFile = false;
77                  else if (arg.equals("-a"))
78                      transferMode = TFTP.ASCII_MODE;
79                  else if (arg.equals("-b"))
80                      transferMode = TFTP.BINARY_MODE;
81                  else
82                  {
83                      System.err.println("Error: unrecognized option.");
84                      System.err.print(USAGE);
85                      System.exit(1);
86                  }
87              }
88              else
89                  break;
90          }
91  
92          // Make sure there are enough arguments
93          if (args.length - argc != 3)
94          {
95              System.err.println("Error: invalid number of arguments.");
96              System.err.print(USAGE);
97              System.exit(1);
98          }
99  
100         // Get host and file arguments
101         hostname = args[argc];
102         localFilename = args[argc + 1];
103         remoteFilename = args[argc + 2];
104 
105         // Create our TFTP instance to handle the file transfer.
106         tftp = new TFTPClient();
107 
108         // We want to timeout if a response takes longer than 60 seconds
109         tftp.setDefaultTimeout(60000);
110 
111         // Open local socket
112         try
113         {
114             tftp.open();
115         }
116         catch (SocketException e)
117         {
118             System.err.println("Error: could not open local UDP socket.");
119             System.err.println(e.getMessage());
120             System.exit(1);
121         }
122 
123         // We haven't closed the local file yet.
124         closed = false;
125 
126         // If we're receiving a file, receive, otherwise send.
127         if (receiveFile)
128         {
129             FileOutputStream output = null;
130             File file;
131 
132             file = new File(localFilename);
133 
134             // If file exists, don't overwrite it.
135             if (file.exists())
136             {
137                 System.err.println("Error: " + localFilename + " already exists.");
138                 System.exit(1);
139             }
140 
141             // Try to open local file for writing
142             try
143             {
144                 output = new FileOutputStream(file);
145             }
146             catch (IOException e)
147             {
148                 tftp.close();
149                 System.err.println("Error: could not open local file for writing.");
150                 System.err.println(e.getMessage());
151                 System.exit(1);
152             }
153 
154             // Try to receive remote file via TFTP
155             try
156             {
157                 tftp.receiveFile(remoteFilename, transferMode, output, hostname);
158             }
159             catch (UnknownHostException e)
160             {
161                 System.err.println("Error: could not resolve hostname.");
162                 System.err.println(e.getMessage());
163                 System.exit(1);
164             }
165             catch (IOException e)
166             {
167                 System.err.println(
168                     "Error: I/O exception occurred while receiving file.");
169                 System.err.println(e.getMessage());
170                 System.exit(1);
171             }
172             finally
173             {
174                 // Close local socket and output file
175                 tftp.close();
176                 try
177                 {
178                     output.close();
179                     closed = true;
180                 }
181                 catch (IOException e)
182                 {
183                     closed = false;
184                     System.err.println("Error: error closing file.");
185                     System.err.println(e.getMessage());
186                 }
187             }
188 
189             if (!closed)
190                 System.exit(1);
191 
192         }
193         else
194         {
195             // We're sending a file
196             FileInputStream input = null;
197 
198             // Try to open local file for reading
199             try
200             {
201                 input = new FileInputStream(localFilename);
202             }
203             catch (IOException e)
204             {
205                 tftp.close();
206                 System.err.println("Error: could not open local file for reading.");
207                 System.err.println(e.getMessage());
208                 System.exit(1);
209             }
210 
211             // Try to send local file via TFTP
212             try
213             {
214                 tftp.sendFile(remoteFilename, transferMode, input, hostname);
215             }
216             catch (UnknownHostException e)
217             {
218                 System.err.println("Error: could not resolve hostname.");
219                 System.err.println(e.getMessage());
220                 System.exit(1);
221             }
222             catch (IOException e)
223             {
224                 System.err.println(
225                     "Error: I/O exception occurred while sending file.");
226                 System.err.println(e.getMessage());
227                 System.exit(1);
228             }
229             finally
230             {
231                 // Close local socket and input file
232                 tftp.close();
233                 try
234                 {
235                     input.close();
236                     closed = true;
237                 }
238                 catch (IOException e)
239                 {
240                     closed = false;
241                     System.err.println("Error: error closing file.");
242                     System.err.println(e.getMessage());
243                 }
244             }
245 
246             if (!closed)
247                 System.exit(1);
248 
249         }
250 
251     }
252 
253 }
254 
255