如何使FTPS数据通道加密

时间:2014-07-08 05:50:02

标签: java ftps

我是FTP新手。我的FTPSClient程序有问题。仅加密控制通道。我需要加密控制和数据通道。

public boolean login(String usrName,char[] passwd){

    try {
        ftpClient = new FTPSClient(protocol,false); // SSL FTP
        ftpClient.connect(strHost);

        int reply = ftpClient.getReplyCode(); // After connection attempt, you should check the reply code to verify success.

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            log.error("FTP server refused connection.");
            return false;
        }

        ftpClient.setBufferSize(1000);

        if (!ftpClient.login(strUsrName,new String(passwd))){
            ftpClient.logout();
            bLoginStatus = false;
            return false;
        }
        else
            bLoginStatus = true;

        if(iMode == LOCAL_ACTIVE_MODE)
            ftpClient.enterLocalActiveMode();
        else if(iMode == LOCAL_PASSIVE_MODE)
            ftpClient.enterLocalPassiveMode();

    }  catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch(Throwable t){
        t.printStackTrace();
    }finally {
        if (bLoginStatus == false && ftpClient.isConnected()){
            try{
                ftpClient.disconnect();
            }
            catch (IOException f){
                // do nothing
            }
        }
    }

    if(ftpClient.isConnected())
        return true;
    else
        return false;
}

1 个答案:

答案 0 :(得分:0)

您需要告诉客户端也加密数据连接,您可以使用PROT命令执行此操作(如果我没记错):

client.execPBSZ(0);
client.execPROT("P");