Copy link to clipboard
Copied
Hi Everyone,
Since i did not find any solution to implement progress in FTP upload via coldfusion, I was trying to use JAVA objects in coldfusion to do that, files are geetting uploaded but i need to override CopyStreamAdapter method in coldfusion to see the bytes transferred. Here is the sample code that i am trying. Please let me know what needs to be done.
<cfoutput>
<form action="" method="post" enctype="multipart/form-data" class="upload-form">
<input type="file" name="fileToUpload" accept="*">
<br>
<input type="submit" value="Upload File" class="submit-button">
</form>
</cfoutput>
<cfscript>
if (structKeyExists(form, "fileToUpload")) {
// FTP server details
ftpServer = "server";
ftpUsername = "user";
ftpPassword = "password";
// Get the uploaded file path
uploadedFilePath = getDirectoryFromPath(cgi.PATH_TRANSLATED);
uploadedFileName = form.fileToUpload;
// Create an FTP connection
ftp = createObject("java", "org.apache.commons.net.ftp.FTPClient");
// Create StreamListener object
streamListener = createObject("java", "org.apache.commons.net.io.CopyStreamAdapter").init();
// Define custom StreamListener to track progress
streamListener.bytesTransferred = function(stream, byTransferred, totalBytes) {
// Calculate progress percentage
percentage = (byTransferred / totalBytes) * 100;
writeOutput("Progress: " & percentage & "%<br>");
};
methods=streamListener.getClass().getDeclaredMethods();
for (method in methods){
WriteOutput("Method:" & method.getName() & "<br>");
}
WriteOutput("<p>Did it succeed? " & streamListener.getClass().getDeclaredMethods() & "<br></p>");
// Set StreamListener on FTP client
ftp.setCopyStreamListener(streamListener);
try {
// Connect to the FTP server
ftp.connect(ftpServer);
result=ftp.login(ftpUsername, ftpPassword);
WriteOutput("<p>Did it succeed? " & result & "<br></p>");
WriteOutput("<h4>Get current directory</h4>");
// Set binary file transfer mode
ftp.setFileType(ftp.BINARY_FILE_TYPE);
// Change to the specified directory on the FTP server
result1=ftp.changeWorkingDirectory("/TestUploadFile");
WriteOutput("<p>Did it succeed? " & result1 & "<br></p>");
// Open the file stream for reading
fileStream = createObject("java", "java.io.FileInputStream").init(uploadedFileName);
WriteOutput("<p>Did it succeed? " & fileStream & "<br></p>");
// Upload the file to the FTP server
result2= ftp.storeFile("TestFileName.zip", fileStream);
WriteOutput("<p>Did it succeed? " & result2 & "<br></p>");
// Close the file stream
fileStream.close();
} catch (any exception) {
// Handle any exceptions that occur
writeOutput("An error occurred: " & exception.message);
}
// Disconnect from the FTP server
ftp.logout();
ftp.disconnect();
}
</cfscript>
Here it says streamListener does not have bytesTransferred method. although when i print class method it has the method. What is the correct way to override this method and implement it here.
Please help. Thanks in advance.
Have something to add?