1

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?

Community
  • 1
  • 1
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • 1
    Do you have access to the FTP server's console/log? Also, does it throw an exception? – Alex Jan 14 '16 at 19:51
  • I don't have access, but I might know someone who does. For the CF Code, given that the output includes "done", from the last `writeoutput` command, I assume there were no exceptions. Also, I did experience exceptions along the way. – Dan Bracuk Jan 14 '16 at 20:06
  • I'ts a shot in the dark, but from my notes we had to set the `javaFtpClient.execPROT("P")` with the server we were working with. You may see something similar in your FileZilla console. – Twillen Jan 14 '16 at 20:13
  • @Twillen, compliments on the accuracy of your shooting in the dark. Addding `javaFtpClient.execPROT("P");` after 'javaFtpClient.enterLocalPassiveMode();' solved the problem. Please post it as an answer. – Dan Bracuk Jan 14 '16 at 20:46
  • @Twillen, aka "FTP sharpshooter" ;-) – Leigh Jan 14 '16 at 21:07

1 Answers1

4

The command javaFtpClient.execProt("P") is required. Setting PROT to "P" sets the Data Channel Protection Level to private.

Twillen
  • 1,458
  • 15
  • 22