ftpclientscriptexample

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ftpclientscriptexample [2022/01/15 23:01]
wikiadmin created
ftpclientscriptexample [2024/12/28 08:36] (current)
wikiadmin
Line 2: Line 2:
  
 This script uses the FTPClient from Apache Commons Net. It includes testing for all errors after each step, except timeouts. fileName and its localPath are passed in as parameters. The return is a String which has the error description in it, or is "Success" if no error. Uses ftp parameters set in ErgoTechConfiguration. properties. This script uses the FTPClient from Apache Commons Net. It includes testing for all errors after each step, except timeouts. fileName and its localPath are passed in as parameters. The return is a String which has the error description in it, or is "Success" if no error. Uses ftp parameters set in ErgoTechConfiguration. properties.
 +
 +The example below is set up as a function. Place the function code at the top of your script, with the main script and code that calls this function below it. For example:
 +
 +<code javascript>
 +
 +function ftpFile (fileName,localPath) {
 +....
 +}
 +
 +//Main script
 +
 +//call the ftp with a filename and path
 +
 +result = ftpFile("myFile.txt","./where/my/file/is/");
 +if (result.equals("Success") {
 +  //file was successfully transferred.  
 +} else {
 +  //there was an error
 +  print(result);
 +}
 +</code>
  
 ====Setup==== ====Setup====
Line 19: Line 40:
  
 If the ftpHostname is blank, ftp will be disabled. If the ftpHostname is blank, ftp will be disabled.
 +
 +====Script for FTP====
  
 <code javascript> <code javascript>
 +
  
  
 //FTP the a file to the server configured in ErgoTechConfiguration.properties //FTP the a file to the server configured in ErgoTechConfiguration.properties
 function ftpFile (fileName,localPath) { function ftpFile (fileName,localPath) {
 +
 +var System = Java.type("java.lang.System");
  
 hostname = System.getProperty("ftpHostname",""); //hostname can be an ip address or a named server as long as the name resolves hostname = System.getProperty("ftpHostname",""); //hostname can be an ip address or a named server as long as the name resolves
Line 72: Line 98:
          
      ftpClient.setDataTimeout(400);      ftpClient.setDataTimeout(400);
 +     // to transfer binary files, use the following call (not required for ASCII transfers).
 +     ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // Set to binary mode
      ftpClient.enterLocalPassiveMode(); //client to server data transfer      ftpClient.enterLocalPassiveMode(); //client to server data transfer
                
Line 93: Line 121:
      }      }
        
- } //end of ftpResults function+ } //end of ftpFile function
 </code> </code>
 +
 +====Running the FTP Client script====
 +
 +The Apache Commons Net 3.x jar (i.e. commons-net-3.1.jar) will need to be on the classpath to run this script. This is included in MIStudio and the TransSECS Devices installation, so this script will run when tested in these environments. To make sure the library is available at runtime, place the Apache Commons Net jar into the Drivers directory of your MIStudio project, or in the addons directory of your TransSECS Devices project.
 +
  • ftpclientscriptexample.1642309290.txt.gz
  • Last modified: 2022/01/15 23:01
  • by wikiadmin