利用FTPSclient 连接FTP服务
public boolean initFtpClient() {
try {
FTPSClient ftpClient=new FTPSClient("TLS");
ftpClient.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftpClient.setControlEncoding("utf-8");
System.out.println("connecting...ftp server:"+this.hostname+":"+this.port);
ftpClient.connect(hostname, port); //连接ftp服务器
ftpClient.execPBSZ(0);// Set protection buffer size
ftpClient.execPROT("P");//Set data channel protection to private--
System.out.println("connect code .."+ftpClient.getReplyCode());
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
if(FTPReply.isPositiveCompletion(replyCode)){
System.out.println("connect successful...ftp server:"+this.hostname+":"+this.port);
//登录ftp服务器
if(ftpClient.login(username.trim(), password.trim())) {
System.out.println("login successfu...ftp server:"+this.hostname+":"+this.port);
ftpClient.enterLocalPassiveMode();
}else {
System.out.println("FTP login failed..."+ftpClient.getReplyCode());
ftpClient.disconnect();
return false;
}
}else {
System.out.println("connect failed...ftp server:"+this.hostname+":"+this.port+"..replyCode is:"+replyCode);
return false;
}
}catch (MalformedURLException e) {
e.printStackTrace();
return false;
}catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}