View Javadoc

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