This is a followup of this question. The correct answer leads to an attempt to use the SFTPClient class in the Apache Commons library.
I can connect. The next step is to upload a file. There is a lot of reference material with sample source code. I used this one as my guide. It's not secure FTP, but it's simple to follow. This is the java code I was attempting to emulate:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FTPUploadFileDemo {
public static void main(String[] args) {
String server = "www.myserver.com";
int port = 21;
String user = "user";
String pass = "pass";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// This was considered unnecessary because I was sending n ASCII file
// APPROACH #1: uploads first file using an InputStream
File firstLocalFile = new File("D:/Test/Projects.zip");
String firstRemoteFile = "Projects.zip";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
and some more code that's not relevent,
This is my ColdFusion equivalent:
localFilename = "d:\dw\dwtest\dan\textfiles\randomText.txt";
remoteFileName = "randomText.txt";
javaFtpClient = CreateObject("java",
"org.apache.commons.net.ftp.FTPSClient").init("SSL", JavaCast("boolean",true));
// note that I am using a secure client
javaInputFile = createObject("java", "java.io.File").init(localFilename);
javaInputStream = createObject("java", "java.io.FileInputStream").init(javaInputFile);
// connect and login
javaFtpClient.connect(JavaCast("string","something"),990);
loginStatus = javaFtpClient.login('valid username','valid password');
writeoutput("login status " & loginStatus & "<br>");
javaFtpClient.enterLocalPassiveMode();
uploadStatus = javaFtpClient.storeFile(remoteFileName, javaInputStream);
writeOutput("upload status " & uploadStatus & "<br>"); javaInputStream.close();
// logout and disconnect
javaFtpClient.logout();
javaFtpClient.disconnect();
writeoutput("done" & "<br>");
The output shows a successful login and an unsuccessful file upload. The lack of file was confirmed using FileZilla.
Can anybody see why the file was not uploaded?