简体   繁体   English

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

[英]Unable to make a connection to FTPS server

I am able to make a connection through FTP to Apache FTP server ( http://mina.apache.org/ftpserver-project/index.html ). 我可以通过FTP与Apache FTP服务器( http://mina.apache.org/ftpserver-project/index.html )建立连接。 I followed the same way to make FTPS Connection. 我遵循相同的方法进行FTPS连接。 but I am getting the following error, when i run the client side program.CAn any one help me in writing client class? 但是当我运行客户端程序时出现以下错误。有人可以帮助我编写客户端类吗?

here is is the code to start server. 这是启动服务器的代码。

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();
    }
}

} }

and My Client class : 和“我的客户”类:

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();
        }
    }
}

} }

u can do it through 你可以做到

1 implicit SSL, first you need to negotiate SSL/TLS , and then the established connection 1个隐式SSL,首先需要协商SSL / TLS,然后再建立连接

2 explicit SSL, as mentioned here 2明确SSL,提到这里

Also, can you use apache-commons FTPSClient ?? 另外,您可以使用apache-commons FTPSClient吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM