List of usage examples for org.apache.commons.net.tftp TFTPClient receiveFile
public int receiveFile(String filename, int mode, OutputStream output, String hostname, int port) throws UnknownHostException, IOException
From source file:com.cablelabs.sim.PCSim2.java
/** * This method creates the T.I.M results file for the a specific * test case.//from w ww . j av a 2 s . com */ private void generateResults(TSDocument doc) { String fileName = doc.getLogFileName(); int index = fileName.lastIndexOf("_ss.log"); String timFileName = fileName.substring(0, index) + "_ss.res"; File tim = new File(timFileName); if (!tim.exists()) { Properties platform = SystemSettings.getSettings(SettingConstants.PLATFORM); Properties dut = SystemSettings.getSettings("DUT"); try { FileOutputStream output = null; if (tim.createNewFile()) { output = new FileOutputStream(tim); } else { output = new FileOutputStream((timFileName + "_" + System.currentTimeMillis())); } String testerName = platform.getProperty(SettingConstants.TESTER_NAME); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date stop = new Date(); String cw = platform.getProperty(SettingConstants.CW_NUMBER); if (!cw.startsWith("CW")) cw = "CW" + cw; String result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<res-document xmlns=\"http://cablelabs.com/TEPResultDocument\" version=\"1.0\">\n\t" + "<execution method=\"automatic\" start=\"" + sdf.format(doc.getStart()) + "\" stop=\"" + sdf.format(stop) + "\" />\n\t" + "<tester username=\"" + testerName + "\" />\n\t" + "<certwave name=\"" + cw + "\" />\n\t" + "<test-result type=\"" + platform.getProperty(SettingConstants.DUT_SUBGROUP) + "\" name=\"" + doc.getNumber() + "\"\n\tproduct=\"" + dut.getProperty(SettingConstants.DUT_VENDOR) + "\" result=\"" + ((testPassed == null || testPassed) ? "PASS" : "FAIL") + "\"\n\tunit=\"" + dut.getProperty(SettingConstants.PRODUCT_UNIT) + "\"/>\n" + "</res-document>"; if (output != null) { output.write(result.getBytes()); output.close(); } // else // logger.fatal(PC2LogCategory.Parser, subCat, // "Couldn't write TIM file! Writing to log file for preservation!\n" + result); } catch (IOException ioe) { logger.error(PC2LogCategory.Parser, subCat, "Could not create new TIM file[" + timFileName + "]."); } String tftpIP = platform.getProperty(SettingConstants.TFTP_SERVER_IP); String tftpPort = platform.getProperty(SettingConstants.TFTP_SERVER_PORT); if (tftpIP != null && tftpPort != null) { boolean recProv = SystemSettings .resolveBooleanSetting(platform.getProperty(SettingConstants.RECORD_PROVISIONING_FILE)); if (recProv && dut != null) try { RecordProvFileListener rpfl = new RecordProvFileListener(); boolean success = rpfl.run(); String provFile = rpfl.getValue(); if (success) { int port = Integer.parseInt(tftpPort); if (port > 0 && port <= 65535) { // Next make sure the TFTP Server IP is not set to 0.0.0.0 if (tftpIP.equals("0.0.0.0")) { logger.warn(PC2LogCategory.PCSim2, subCat, "The TFTP Server IP setting in the platform file is not valid. Ending auto generate operation."); } else { // Next we need to TFTP the file from the server TFTPClient client = new TFTPClient(); int dirIndex = fileName.lastIndexOf("/", index); File dir = new File(fileName.substring(0, dirIndex) + PROV_FILE_DIRECTORY); if (dir.exists() && !dir.isDirectory()) { logger.error(PC2LogCategory.PCSim2, subCat, "The path " + dir.getPath() + " is not a directory. Terminating the recording of the provisioning file."); } File binFile = new File(fileName.substring(0, dirIndex + 1) + PROV_FILE_DIRECTORY + File.separator + fileName.substring(dirIndex + 1, index) + "_prov.bin"); boolean exists = false; if (!binFile.exists()) exists = binFile.createNewFile(); if (exists && binFile.canWrite()) { FileOutputStream ostrm = new FileOutputStream(binFile); //InetAddress ia = InetAddress.getByName("10.4.1.37"); client.open(); // client.open(20003, ia); client.receiveFile(provFile, TFTP.BINARY_MODE, ostrm, tftpIP, port); client.close(); logger.info(PC2LogCategory.PCSim2, subCat, "TFTP of the record provisioning file is complete."); } else { logger.warn(PC2LogCategory.PCSim2, subCat, "The system could not TFTP the provisioning file because TFTP address is " + tftpIP + "."); } } } else { logger.warn(PC2LogCategory.PCSim2, subCat, "Recording of the provisioning file is terminating because the port(" + port + ") is less than 0 or greater than 65535."); } } else { logger.warn(PC2LogCategory.PCSim2, subCat, "Recording of the provisioning file is terminating because PACT returned an error string of \"" + provFile + "\"."); } } catch (NumberFormatException nfe) { logger.warn(PC2LogCategory.PCSim2, subCat, "PCSim2 is not auto generating a provisioning file because the " + "TFTP Server Port setting doesn't appear to be a number."); } catch (UnknownHostException uhe) { logger.warn(PC2LogCategory.PCSim2, subCat, "PCSim2 is not auto generating a provisioning file because the " + "system encountered an error when attempting to send the file to the TFTP Server.\n" + uhe.getMessage() + "\n" + uhe.getStackTrace()); } catch (IOException ioe) { logger.warn(PC2LogCategory.PCSim2, subCat, "PCSim2 is not auto generating a provisioning file because the " + "system encountered an error when attempting to send the file to the TFTP Server.\n" + ioe.getMessage() + "\n" + ioe.getStackTrace()); } } } }
From source file:org.apache.commons.net.examples.ftp.TFTPExample.java
private static boolean receive(int transferMode, String hostname, String localFilename, String remoteFilename, TFTPClient tftp) { boolean closed; FileOutputStream output = null; File file;//w w w. j a v a2 s. c om file = new File(localFilename); // If file exists, don't overwrite it. if (file.exists()) { System.err.println("Error: " + localFilename + " already exists."); return false; } // Try to open local file for writing try { output = new FileOutputStream(file); } catch (IOException e) { tftp.close(); throw new RuntimeException("Error: could not open local file for writing.", e); } open(tftp); // Try to receive remote file via TFTP try { String[] parts = hostname.split(":"); if (parts.length == 2) { tftp.receiveFile(remoteFilename, transferMode, output, parts[0], Integer.parseInt(parts[1])); } else { tftp.receiveFile(remoteFilename, transferMode, output, hostname); } } catch (UnknownHostException e) { System.err.println("Error: could not resolve hostname."); System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println("Error: I/O exception occurred while receiving file."); System.err.println(e.getMessage()); System.exit(1); } finally { // Close local socket and output file closed = close(tftp, output); } return closed; }
From source file:person.daizhongde.virtue.util.test.TFTPExample.java
private static boolean receive(int transferMode, String hostname, String localFilename, String remoteFilename, TFTPClient tftp) { boolean closed; FileOutputStream output = null; File file;/* w w w . ja v a 2s .c om*/ file = new File(localFilename); // If file exists, don't overwrite it. if (file.exists()) { System.err.println("Error: " + localFilename + " already exists."); System.exit(1); } // Try to open local file for writing try { output = new FileOutputStream(file); } catch (IOException e) { tftp.close(); System.err.println("Error: could not open local file for writing."); System.err.println(e.getMessage()); System.exit(1); } open(tftp); // Try to receive remote file via TFTP try { String[] parts = hostname.split(":"); if (parts.length == 2) { tftp.receiveFile(remoteFilename, transferMode, output, parts[0], Integer.parseInt(parts[1])); } else { tftp.receiveFile(remoteFilename, transferMode, output, hostname); } } catch (UnknownHostException e) { System.err.println("Error: could not resolve hostname."); System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println("Error: I/O exception occurred while receiving file."); System.err.println(e.getMessage()); System.exit(1); } finally { // Close local socket and output file closed = close(tftp, output); } return closed; }