1 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 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 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 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 hostname = args[argc]; 102 localFilename = args[argc + 1]; 103 remoteFilename = args[argc + 2]; 104 105 tftp = new TFTPClient(); 107 108 tftp.setDefaultTimeout(60000); 110 111 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 closed = false; 125 126 if (receiveFile) 128 { 129 FileOutputStream output = null; 130 File file; 131 132 file = new File (localFilename); 133 134 if (file.exists()) 136 { 137 System.err.println("Error: " + localFilename + " already exists."); 138 System.exit(1); 139 } 140 141 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 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 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 FileInputStream input = null; 197 198 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 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 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 256 | Popular Tags |