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  
18  package org.apache.commons.net.examples.ftp;
19  
20  import java.io.Closeable;
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.net.SocketException;
26  import java.net.UnknownHostException;
27  import java.time.Duration;
28  
29  import org.apache.commons.net.tftp.TFTP;
30  import org.apache.commons.net.tftp.TFTPClient;
31  import org.apache.commons.net.tftp.TFTPPacket;
32  
33  /**
34   * This is an example of a simple Java TFTP client. Notice how all the code is really just argument processing and error handling.
35   * <p>
36   * Usage: TFTPExample [options] hostname localfile remotefile hostname - The name of the remote host, with optional :port localfile - The name of the local file
37   * to send or the name to use for the received file remotefile - The name of the remote file to receive or the name for the remote server to use to name the
38   * local file being sent. options: (The default is to assume -r -b) -s Send a local file -r Receive a remote file -a Use ASCII transfer mode -b Use binary
39   * transfer mode.
40   * </p>
41   */
42  public final class TFTPExample {
43      static final String USAGE = "Usage: TFTPExample [options] hostname localfile remotefile\n\n" + "hostname   - The name of the remote host [:port]\n"
44              + "localfile  - The name of the local file to send or the name to use for\n" + "\tthe received file\n"
45              + "remotefile - The name of the remote file to receive or the name for\n" + "\tthe remote server to use to name the local file being sent.\n\n"
46              + "options: (The default is to assume -r -b)\n" + "\t-t timeout in seconds (default 60s)\n" + "\t-s Send a local file\n"
47              + "\t-r Receive a remote file\n" + "\t-a Use ASCII transfer mode\n" + "\t-b Use binary transfer mode\n" + "\t-v Verbose (trace packets)\n";
48  
49      private static boolean close(final TFTPClient tftp, final Closeable output) {
50          boolean closed;
51          tftp.close();
52          try {
53              if (output != null) {
54                  output.close();
55              }
56              closed = true;
57          } catch (final IOException e) {
58              closed = false;
59              System.err.println("Error: error closing file.");
60              System.err.println(e.getMessage());
61          }
62          return closed;
63      }
64  
65      public static void main(final String[] args) throws IOException {
66          boolean receiveFile = true, closed;
67          int transferMode = TFTP.BINARY_MODE, argc;
68          String arg;
69          final String hostname;
70          final String localFilename;
71          final String remoteFilename;
72          final TFTPClient tftp;
73          int timeout = 60000;
74          boolean verbose = false;
75  
76          // Parse options
77          for (argc = 0; argc < args.length; argc++) {
78              arg = args[argc];
79              if (!arg.startsWith("-")) {
80                  break;
81              }
82              if (arg.equals("-r")) {
83                  receiveFile = true;
84              } else if (arg.equals("-s")) {
85                  receiveFile = false;
86              } else if (arg.equals("-a")) {
87                  transferMode = TFTP.ASCII_MODE;
88              } else if (arg.equals("-b")) {
89                  transferMode = TFTP.BINARY_MODE;
90              } else if (arg.equals("-t")) {
91                  timeout = 1000 * Integer.parseInt(args[++argc]);
92              } else if (arg.equals("-v")) {
93                  verbose = true;
94              } else {
95                  System.err.println("Error: unrecognized option.");
96                  System.err.print(USAGE);
97                  System.exit(1);
98              }
99          }
100 
101         // Make sure there are enough arguments
102         if (args.length - argc != 3) {
103             System.err.println("Error: invalid number of arguments.");
104             System.err.print(USAGE);
105             System.exit(1);
106         }
107 
108         // Get host and file arguments
109         hostname = args[argc];
110         localFilename = args[argc + 1];
111         remoteFilename = args[argc + 2];
112 
113         // Create our TFTP instance to handle the file transfer.
114         if (verbose) {
115             tftp = new TFTPClient() {
116                 @Override
117                 protected void trace(final String direction, final TFTPPacket packet) {
118                     System.out.println(direction + " " + packet);
119                 }
120             };
121         } else {
122             tftp = new TFTPClient();
123         }
124 
125         // We want to timeout if a response takes longer than 60 seconds
126         tftp.setDefaultTimeout(Duration.ofSeconds(timeout));
127 
128         // We haven't closed the local file yet.
129         closed = false;
130 
131         // If we're receiving a file, receive, otherwise send.
132         if (receiveFile) {
133             closed = receive(transferMode, hostname, localFilename, remoteFilename, tftp);
134         } else {
135             // We're sending a file
136             closed = send(transferMode, hostname, localFilename, remoteFilename, tftp);
137         }
138 
139         System.out.println("Recd: " + tftp.getTotalBytesReceived() + " Sent: " + tftp.getTotalBytesSent());
140 
141         if (!closed) {
142             System.out.println("Failed");
143             System.exit(1);
144         }
145 
146         System.out.println("OK");
147     }
148 
149     private static void open(final TFTPClient tftp) throws IOException {
150         try {
151             tftp.open();
152         } catch (final SocketException e) {
153             throw new IOException("Error: could not open local UDP socket.", e);
154         }
155     }
156 
157     private static boolean receive(final int transferMode, final String hostname, final String localFilename, final String remoteFilename,
158             final TFTPClient tftp) throws IOException {
159         final boolean closed;
160         FileOutputStream output;
161         final File file;
162 
163         file = new File(localFilename);
164 
165         // If file exists, don't overwrite it.
166         if (file.exists()) {
167             System.err.println("Error: " + localFilename + " already exists.");
168             return false;
169         }
170 
171         // Try to open local file for writing
172         try {
173             output = new FileOutputStream(file);
174         } catch (final IOException e) {
175             tftp.close();
176             throw new IOException("Error: could not open local file for writing.", e);
177         }
178 
179         open(tftp);
180 
181         // Try to receive remote file via TFTP
182         try {
183             final String[] parts = hostname.split(":");
184             if (parts.length == 2) {
185                 tftp.receiveFile(remoteFilename, transferMode, output, parts[0], Integer.parseInt(parts[1]));
186             } else {
187                 tftp.receiveFile(remoteFilename, transferMode, output, hostname);
188             }
189         } catch (final UnknownHostException e) {
190             System.err.println("Error: could not resolve hostname.");
191             System.err.println(e.getMessage());
192             System.exit(1);
193         } catch (final IOException e) {
194             System.err.println("Error: I/O exception occurred while receiving file.");
195             System.err.println(e.getMessage());
196             System.exit(1);
197         } finally {
198             // Close local socket and output file
199             closed = close(tftp, output);
200         }
201 
202         return closed;
203     }
204 
205     private static boolean send(final int transferMode, final String hostname, final String localFilename, final String remoteFilename, final TFTPClient tftp)
206             throws IOException {
207         final boolean closed;
208         FileInputStream input;
209 
210         // Try to open local file for reading
211         try {
212             input = new FileInputStream(localFilename);
213         } catch (final IOException e) {
214             tftp.close();
215             throw new IOException("Error: could not open local file for reading.", e);
216         }
217 
218         open(tftp);
219 
220         // Try to send local file via TFTP
221         try {
222             final String[] parts = hostname.split(":");
223             if (parts.length == 2) {
224                 tftp.sendFile(remoteFilename, transferMode, input, parts[0], Integer.parseInt(parts[1]));
225             } else {
226                 tftp.sendFile(remoteFilename, transferMode, input, hostname);
227             }
228         } catch (final UnknownHostException e) {
229             System.err.println("Error: could not resolve hostname.");
230             System.err.println(e.getMessage());
231             System.exit(1);
232         } catch (final IOException e) {
233             System.err.println("Error: I/O exception occurred while sending file.");
234             System.err.println(e.getMessage());
235             System.exit(1);
236         } finally {
237             // Close local socket and input file
238             closed = close(tftp, input);
239         }
240 
241         return closed;
242     }
243 
244 }