无法建立与FTPS服务器的连接

时间:2013-02-08 09:41:25

标签: java apache file ftps b2b

我能够通过FTP连接到Apache FTP服务器(http://mina.apache.org/ftpserver-project/index.html)。我按照相同的方式进行FTPS连接。但是当我运行客户端程序时,我收到以下错误。有没有人帮我编写客户端类?

这是启动服务器的代码。

public class FTPSServer {
public static void main(String[] args) {
    FtpServerFactory serverFactory = new FtpServerFactory();
    ListenerFactory factory = new ListenerFactory();
    // set the port of the listener
    factory.setPort(2221);
    // define SSL configuration
    SslConfigurationFactory ssl = new SslConfigurationFactory();
    ssl.setKeystoreFile(new File(
            "src/ftp/resources/cert.jks"));
    ssl.setKeystorePassword("password");
    // set the SSL configuration for the listener
    factory.setSslConfiguration(ssl.createSslConfiguration());
    factory.setImplicitSsl(true);
    // replace the default listener
    serverFactory.addListener("default", factory.createListener());

    PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

    userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
    UserManager userManager = userManagerFactory.createUserManager();

    BaseUser user = new BaseUser();
    user.setName("test");
    user.setPassword("test");
    user.setHomeDirectory("D:\\FTP-TEST-UPLOADS");
    List<Authority> auths = new ArrayList<Authority>();
    Authority auth = new WritePermission();
    auths.add(auth);
    user.setAuthorities(auths);
    try {
        userManager.save(user);
    } catch (FtpException e1) {
        e1.printStackTrace();
    }
    serverFactory.setUserManager(userManagerFactory.createUserManager());
    // start the server
    FtpServer server = serverFactory.createServer();
    try {
        server.start();
        System.out.println("FTPs Server Started");
    } catch (FtpException e) {
        e.printStackTrace();
    }
}

}

和我的客户端类:

class FtpClient {
private static String ftpServerAddress = "localhost";
private static int port = 2121;
private static String userName = "test";
private static String password = "test";

public static void main(String[] args) {
    try {
        uploadFile();
        downloadFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void downloadFile() throws IOException {
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;
    boolean result;

    try {
        client.connect(ftpServerAddress, port);

        result = client.login(userName, password);

        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }

        if (result == true) {
            System.out.println("Successfully logged in!");
        } else {
            System.out.println("Login Fail!");
            return;
        }

        // The remote filename to be downloaded.
        String filename = "Technolabs Logo.PNG";
        fos = new FileOutputStream(filename);

        // Download file from FTP server

        result = client.retrieveFile("receivedFiles/" + filename, fos);
        if (result == true)
            System.out.println("File was downloaded");
        client.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            client.disconnect();
            fos.close();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private static void uploadFile() throws IOException {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;
    boolean result;

    try {
        client.connect(ftpServerAddress, port);

        result = client.login(userName, password);

        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }

        if (result == true)
            System.out.println("Successfully logged in!");
        else {
            System.out.println("Login Fail!");
            return;
        }
        client.setFileType(FTP.BINARY_FILE_TYPE);

        client.enterLocalPassiveMode();

        client.changeWorkingDirectory("/");

        File file = new File("D:/myFile.PNG");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = client.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        client.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            client.disconnect();
            fis.close();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

你可以通过

来做到这一点

1隐式SSL, 首先,您需要协商SSL / TLS,然后建立连接

2显式SSL,如上所述here

另外,你可以使用apache-commons FTPSClient ??