Skip to content

Instantly share code, notes, and snippets.

@GiancarloGomez
Last active August 30, 2019 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GiancarloGomez/6fc90942c56d5743ef61a6ea010b5613 to your computer and use it in GitHub Desktop.
Save GiancarloGomez/6fc90942c56d5743ef61a6ea010b5613 to your computer and use it in GitHub Desktop.
ColdFusion-org.apache.commons.net.ftp.FTPSClient-Example
try{
ftpsClient = CreateObject("java","org.apache.commons.net.ftp.FTPSClient").init(JavaCast("boolean",true));
// set server address to connect to and begin
ftpsClient.connect("yourserver.com",990);
// set credentials
loggedIn = ftpsClient.login("username","password");
if (loggedIn){
//enter passive mode
ftpsClient.enterLocalPassiveMode();
// Set protection buffer size
ftpsClient.execPBSZ(0);
// Set data channel protection to private
ftpsClient.execPROT("P");
/* ==========================================================================
* LIST DIRECTORIES
* First loop returns instance of org.apache.commons.net.ftp.FTPFile
* Second loop returns strings
========================================================================== */
writeOutput("<ul>");
for (_file in ftpsClient.listDirectories())
writeOutput("<li>" & _file.getName() & "</li>");
writeOutput("</ul>");
writeOutput("<ul>");
for (_file in ftpsClient.listNames())
writeOutput("<li>" & _file & "</li>");
writeOutput("</ul>");
/* ==========================================================================
* LIST FILES
* First loop returns instance of org.apache.commons.net.ftp.FTPFile
* Second loop returns strings
========================================================================== */
writeOutput("<ul>");
for (_file in ftpsClient.listFiles("/_offline"))
writeOutput("<li>" & _file.getName() & "</li>");
writeOutput("</ul>");
writeOutput("<ul>");
for (_file in ftpsClient.listNames("/_offline"))
writeOutput("<li>" & _file & "</li>");
writeOutput("</ul>");
/* ==========================================================================
* DOWNLOAD A FILE
* In this example I am working with a file in the remote server which is
* located @ /_offline/index.html which I want to download to the directory
* in which this file is executing
========================================================================== */
localFile = getDirectoryFromPath(getCurrentTemplatePath()) & "index.html";
remoteFile = "/_offline/index.html";
// create the buffered output stream for download
downloadFile = createObject("java", "java.io.File").init(localFile);
fileOutputStream = createObject("java", "java.io.FileOutputStream").init(downloadFile);
bufferedOutputStream = createObject("java", "java.io.BufferedOutputStream").init(fileOutputStream);
// download file
success = ftpsClient.retrieveFile(remoteFile, bufferedOutputStream);
// close stream
bufferedOutputStream.close();
if (success)
writeOutput("<p>FILE DOWNLOADED</p>");
/* ==========================================================================
* UPLOAD A FILE
* In this example I am working with an image file on my local machine which
* I am uploading to the /_offline directory. Please note that because it is
* an image file I had to set the file type in order for the upload to not end
* up corrupted. I believe this is not necessary if working with text files.
========================================================================== */
localFile = getDirectoryFromPath(getCurrentTemplatePath()) & "image-01.png";
remoteFile = "/_offline/image-01.png";
// create input stream for upload
uploadFile = createObject("java", "java.io.File").init(localFile);
fileInputStream = createObject("java", "java.io.FileInputStream").init(uploadFile);
// important if uploading an image file
ftpsClient.setFileType(ftpsClient.BINARY_FILE_TYPE);
// upload file
success = ftpsClient.storeFile(remoteFile, fileInputStream);
// close stream
fileInputStream.close();
if (success)
writeOutput("<p>FILE UPLOADED</p>");
}
if (ftpsClient.isConnected()){
ftpsClient.logout();
ftpsClient.disconnect();
}
}
catch (any e){
writeDump(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment