使用Java FTP客户端从ColdFusion 9安全FTP

时间:2016-01-14 19:25:17

标签: java coldfusion ftp sftp

这是this question的后续内容。正确的答案导致尝试在Apache Commons库中使用SFTPClient类。

我可以连接。下一步是上传文件。有很多参考资料和样本源代码。我使用this one作为我的向导。这不是安全的FTP,但它很容易遵循。这是我试图模仿的java代码:

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, 

这是我的ColdFusion等价物:

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>");

输出显示成功登录和文件上传失败。使用FileZilla确认缺少文件。

有人可以看到文件没有上传的原因吗?

1 个答案:

答案 0 :(得分:4)

命令javaFtpClient.execProt("P")是必需的。将PROT设置为“P”会将数据通道保护级别设置为私有。