Best Solutions: Connecting to FTP via JavaScript in NeoLoad

Overview

During your load test, you may want your virtual user to connect to an FTP server to upload or download some files. This can be done through a NeoLoad JavaScript. Here is an example of how to use FTP within a JavaScript using the FTPClient packages. FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server.

We have provided the basics to send and receive a file, create and change directories, etc.

See a full list of FTPClient commands: https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#changeWorkingDirectory(java.lang.String).

The Best Solution for you to Consider

Copy and paste these code snippets into a skeleton JavaScript in your user path.

NOTE: It is always required to have the Initialization routine placed first to connect to your FTP server before performing the additional actions.  Also, note that it’s recommended to include myFTPClient.logout(); and myFTPClient.disconnect(); at the end of your script to close your session’s connection.

// Initialization (required to connect, fill in servername, username and password)

myFTPClient =new Packages.org.apache.commons.net.ftp.FTPClient(); myFTPClient.connect(“servername”); myFTPClient.login(“username”,”password”); logger.debug(myFTPClient.getReplyCode()+” “+myFTPClient.getReplyString());

//Create a new folder with MKDIR command

myFTPClient.makeDirectory(“FTPNeoload”); logger.debug(myFTPClient.getReplyCode()+” “+myFTPClient.getReplyString());

//Change directory command
myFTPClient.changeWorkingDirectory(“FTPNeoload”); logger.debug(myFTPClient.getReplyCode()+” “+myFTPClient.getReplyString());

NOTE: Using PUT or GET methods may require switching your FTP connection into passive transfer mode. To do that add this one line command:

// Put FTP file transfer into passive mode

myFTPClient.enterLocalPassiveMode();

//Use of the GET method

var writer = new java.io.FileOutputStream(“c:\\Temp\\MyFile.zip”,true);

myFTPClient.retrieveFile(“MyFile.zip”,writer);

writer.close();

logger.debug(myFTPClient.getReplyCode()+” “+myFTPClient.getReplyString());

//Use of the PUT method

var reader = new java.io.FileInputStream(“c:\\Temp\\myfile.xps”); myFTPClient.storeFile(“myfile.xps”,reader);

reader.close();

logger.debug(myFTPClient.getReplyCode()+” “+myFTPClient.getReplyString());

//Command PWD (Print Working Directory)

myFTPClient.printWorkingDirectory(); logger.debug(myFTPClient.getReplyCode()+” “+myFTPClient.getReplyString());

//Command LIST

engine =myFTPClient.initiateListParsing(“/”);

while (engine.hasNext())

{

file = engine.getNext(5);

logger.debug(file[0].getName());

}

For more information, click here.

Next Steps: Review Documentation or Talk to Us

  • Check out all of the content included within NeoLoad documentation.
  • Want to discuss further, contact support.

Learn More about Performance Testing & Load Testing

Discover more load testing and performance testing content on the Neotys Resources pages, or download the latest version of NeoLoad and start testing today.

Leave a Reply

Your email address will not be published. Required fields are marked *