Table of Contents

Script Example: FTP Client to transfer a file to an FTP Server

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:

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);
}

Setup

This script will read system properties set up when MIStudio, MIX, or TransSECS starts up. These properties are added to the ErgoTechConfiguration.properties file (or mix.properties if running MIX).

Add these lines to the ErgoTechConfiguration.properties file:

#for ftp
ftpHostname = hostname 
ftpPort = 21
ftpLogin = admin
ftpPassword = admin
ftpDestDir = somepath/on/ftpserver

If the ftpHostname is blank, ftp will be disabled.

Script for FTP

 
 
//FTP the a file to the server configured in ErgoTechConfiguration.properties
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
 
if (hostname.length>0) {//if ftpHostname is not set, do not try to ftp   
 
    port = System.getProperty("ftpPort",21);//default to 21 if not set
    login = System.getProperty("ftpLogin");
    password = System.getProperty("ftpPassword");
    destinationDir = System.getProperty("ftpDestDir",".");
 
    var FTPClient = Java.type("org.apache.commons.net.ftp.FTPClient");
    var FileInputStream = Java.type("java.io.FileInputStream");
 
 
    ftpClient = new FTPClient();
    ftpClient.setDataTimeout(5000);
    ftpClient.setDefaultTimeout(5000);
    ftpClient.setConnectTimeout(5000);   
 
    try {
       ftpClient.connect(hostname,port);      
    } catch (e) {
     //exception can be  IOException or UnknownHostException
     if (e instanceof java.io.IOException) {
         return ("Could not connect to ftp server "+hostname+" on port "+port);
      } else if (e instanceof java.net.UnknownHostException) {
          return ("Unknown host "+hostname);
        }
        return ("Unknown connection error to host "+hostname+" on port "+port);
     } 
 
    result=ftpClient.login(login,password);
    if (result==0) { //could not login
       ftpClient.disconnect();
       return("Could not log in to ftp server (invalid username or password)");        
    }
 
    result = ftpClient.changeWorkingDirectory(destinationDir);
 
     if ( result==0) {
     //directory did not exist probably
     ftpClient.logout();
     ftpClient.disconnect();
         return("Could not change to directory "+destinationDir+" on ftp server");      
     }
 
     ftpClient.setDataTimeout(400);
     ftpClient.enterLocalPassiveMode(); //client to server data transfer
 
     //transfer the file
     is = new FileInputStream(localPath + fileName);
 
     result = ftpClient.storeFile(fileName,is);
     if (result==0) {
        is.close();       
        ftpClient.logout();
        ftpClient.disconnect();
        return("FTP transfer of file "+fileName+" failed after successful login");  
     }
 
     is.close();       
     ftpClient.logout();
     ftpClient.disconnect();
     return("Success");
    } else {
        return("ftpHostname is not set in ErgoTechConfiguration.properties, not transferring file " + fileName);
     }
 
 } //end of ftpFile function

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.