| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| examples.tftp |
|
|
| 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 | 0 | 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 | 0 | boolean receiveFile = true, closed; |
| 63 | 0 | int transferMode = TFTP.BINARY_MODE, argc; |
| 64 | String arg, hostname, localFilename, remoteFilename; |
|
| 65 | TFTPClient tftp; |
|
| 66 | ||
| 67 | // Parse options |
|
| 68 | 0 | for (argc = 0; argc < args.length; argc++) |
| 69 | { |
|
| 70 | 0 | arg = args[argc]; |
| 71 | 0 | if (arg.startsWith("-")) |
| 72 | { |
|
| 73 | 0 | if (arg.equals("-r")) |
| 74 | 0 | receiveFile = true; |
| 75 | 0 | else if (arg.equals("-s")) |
| 76 | 0 | receiveFile = false; |
| 77 | 0 | else if (arg.equals("-a")) |
| 78 | 0 | transferMode = TFTP.ASCII_MODE; |
| 79 | 0 | else if (arg.equals("-b")) |
| 80 | 0 | transferMode = TFTP.BINARY_MODE; |
| 81 | else |
|
| 82 | { |
|
| 83 | 0 | System.err.println("Error: unrecognized option."); |
| 84 | 0 | System.err.print(USAGE); |
| 85 | 0 | System.exit(1); |
| 86 | } |
|
| 87 | } |
|
| 88 | else |
|
| 89 | break; |
|
| 90 | } |
|
| 91 | ||
| 92 | // Make sure there are enough arguments |
|
| 93 | 0 | if (args.length - argc != 3) |
| 94 | { |
|
| 95 | 0 | System.err.println("Error: invalid number of arguments."); |
| 96 | 0 | System.err.print(USAGE); |
| 97 | 0 | System.exit(1); |
| 98 | } |
|
| 99 | ||
| 100 | // Get host and file arguments |
|
| 101 | 0 | hostname = args[argc]; |
| 102 | 0 | localFilename = args[argc + 1]; |
| 103 | 0 | remoteFilename = args[argc + 2]; |
| 104 | ||
| 105 | // Create our TFTP instance to handle the file transfer. |
|
| 106 | 0 | tftp = new TFTPClient(); |
| 107 | ||
| 108 | // We want to timeout if a response takes longer than 60 seconds |
|
| 109 | 0 | tftp.setDefaultTimeout(60000); |
| 110 | ||
| 111 | // Open local socket |
|
| 112 | try |
|
| 113 | { |
|
| 114 | 0 | tftp.open(); |
| 115 | } |
|
| 116 | 0 | catch (SocketException e) |
| 117 | { |
|
| 118 | 0 | System.err.println("Error: could not open local UDP socket."); |
| 119 | 0 | System.err.println(e.getMessage()); |
| 120 | 0 | System.exit(1); |
| 121 | 0 | } |
| 122 | ||
| 123 | // We haven't closed the local file yet. |
|
| 124 | 0 | closed = false; |
| 125 | ||
| 126 | // If we're receiving a file, receive, otherwise send. |
|
| 127 | 0 | if (receiveFile) |
| 128 | { |
|
| 129 | 0 | FileOutputStream output = null; |
| 130 | File file; |
|
| 131 | ||
| 132 | 0 | file = new File(localFilename); |
| 133 | ||
| 134 | // If file exists, don't overwrite it. |
|
| 135 | 0 | if (file.exists()) |
| 136 | { |
|
| 137 | 0 | System.err.println("Error: " + localFilename + " already exists."); |
| 138 | 0 | System.exit(1); |
| 139 | } |
|
| 140 | ||
| 141 | // Try to open local file for writing |
|
| 142 | try |
|
| 143 | { |
|
| 144 | 0 | output = new FileOutputStream(file); |
| 145 | } |
|
| 146 | 0 | catch (IOException e) |
| 147 | { |
|
| 148 | 0 | tftp.close(); |
| 149 | 0 | System.err.println("Error: could not open local file for writing."); |
| 150 | 0 | System.err.println(e.getMessage()); |
| 151 | 0 | System.exit(1); |
| 152 | 0 | } |
| 153 | ||
| 154 | // Try to receive remote file via TFTP |
|
| 155 | try |
|
| 156 | { |
|
| 157 | 0 | tftp.receiveFile(remoteFilename, transferMode, output, hostname); |
| 158 | 0 | } |
| 159 | 0 | catch (UnknownHostException e) |
| 160 | { |
|
| 161 | 0 | System.err.println("Error: could not resolve hostname."); |
| 162 | 0 | System.err.println(e.getMessage()); |
| 163 | 0 | System.exit(1); |
| 164 | 0 | } |
| 165 | 0 | catch (IOException e) |
| 166 | { |
|
| 167 | 0 | System.err.println( |
| 168 | "Error: I/O exception occurred while receiving file."); |
|
| 169 | 0 | System.err.println(e.getMessage()); |
| 170 | 0 | System.exit(1); |
| 171 | 0 | } |
| 172 | finally |
|
| 173 | { |
|
| 174 | // Close local socket and output file |
|
| 175 | 0 | tftp.close(); |
| 176 | try |
|
| 177 | { |
|
| 178 | 0 | output.close(); |
| 179 | 0 | closed = true; |
| 180 | } |
|
| 181 | 0 | catch (IOException e) |
| 182 | { |
|
| 183 | 0 | closed = false; |
| 184 | 0 | System.err.println("Error: error closing file."); |
| 185 | 0 | System.err.println(e.getMessage()); |
| 186 | 0 | } |
| 187 | 0 | } |
| 188 | ||
| 189 | 0 | if (!closed) |
| 190 | 0 | System.exit(1); |
| 191 | ||
| 192 | } |
|
| 193 | else |
|
| 194 | { |
|
| 195 | // We're sending a file |
|
| 196 | 0 | FileInputStream input = null; |
| 197 | ||
| 198 | // Try to open local file for reading |
|
| 199 | try |
|
| 200 | { |
|
| 201 | 0 | input = new FileInputStream(localFilename); |
| 202 | } |
|
| 203 | 0 | catch (IOException e) |
| 204 | { |
|
| 205 | 0 | tftp.close(); |
| 206 | 0 | System.err.println("Error: could not open local file for reading."); |
| 207 | 0 | System.err.println(e.getMessage()); |
| 208 | 0 | System.exit(1); |
| 209 | 0 | } |
| 210 | ||
| 211 | // Try to send local file via TFTP |
|
| 212 | try |
|
| 213 | { |
|
| 214 | 0 | tftp.sendFile(remoteFilename, transferMode, input, hostname); |
| 215 | 0 | } |
| 216 | 0 | catch (UnknownHostException e) |
| 217 | { |
|
| 218 | 0 | System.err.println("Error: could not resolve hostname."); |
| 219 | 0 | System.err.println(e.getMessage()); |
| 220 | 0 | System.exit(1); |
| 221 | 0 | } |
| 222 | 0 | catch (IOException e) |
| 223 | { |
|
| 224 | 0 | System.err.println( |
| 225 | "Error: I/O exception occurred while sending file."); |
|
| 226 | 0 | System.err.println(e.getMessage()); |
| 227 | 0 | System.exit(1); |
| 228 | 0 | } |
| 229 | finally |
|
| 230 | { |
|
| 231 | // Close local socket and input file |
|
| 232 | 0 | tftp.close(); |
| 233 | try |
|
| 234 | { |
|
| 235 | 0 | input.close(); |
| 236 | 0 | closed = true; |
| 237 | } |
|
| 238 | 0 | catch (IOException e) |
| 239 | { |
|
| 240 | 0 | closed = false; |
| 241 | 0 | System.err.println("Error: error closing file."); |
| 242 | 0 | System.err.println(e.getMessage()); |
| 243 | 0 | } |
| 244 | 0 | } |
| 245 | ||
| 246 | 0 | if (!closed) |
| 247 | 0 | System.exit(1); |
| 248 | ||
| 249 | } |
|
| 250 | ||
| 251 | 0 | } |
| 252 | ||
| 253 | } |
|
| 254 | ||
| 255 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |