importsun.net.ftp.FtpClient;importjava.net.Socket;importjava.io.IOException;publicclassPasvFtpClientextendsFtpClient{/*** FTP服务器的地址*/privateString serverAddr;/*** 连接到FTP服务器的Socket*/privateSocket socket;/*** 仿造父类定义的静态变量*/protectedfinalstaticintFTP_ERROR=3;/*** 仿造父类定义的静态变量*/protectedfinalstaticintFTP_SUCCESS=1;publicPasvFtpClient(String s)throwsIOException{super(s);
serverAddr=s;
socket=null;
}publicPasvFtpClient(String s,inti)throwsIOException{super(s,i);
serverAddr=s;
socket=null;
}publicPasvFtpClient(){super();
socket=null;
}/*** 复写的主要部分,父类采用PORT模式,这里改为PASV模式
*@params 传入的FTP命令
*@return连接到FTP服务器的Socket
*@throwsIOException*/protectedSocket openDataConnection(String s)throwsIOException{if(socket==null){
String s1="PASV";if(issueCommand(s1)==FTP_ERROR){
MyFtpProtocolException ftpprotocolexception=newMyFtpProtocolException("PASV");throwftpprotocolexception;
}
String responseStr=this.getResponseString();intlocation=responseStr.lastIndexOf(",");intn=Integer.parseInt(responseStr.substring(location+1,
responseStr.indexOf(")")));
responseStr=responseStr.substring(0,location);
location=responseStr.lastIndexOf(",");intm=Integer.parseInt(responseStr.substring(location+1,
responseStr.length()));
socket=newSocket(serverAddr,m*256+n);
}if(issueCommand(s)==FTP_ERROR){
MyFtpProtocolException ftpprotocolexception1=newMyFtpProtocolException(s);throwftpprotocolexception1;
}returnsocket;
}/*** 关闭与FTP服务器的连接
*@throwsIOException*/publicvoidcloseServer()throwsIOException{
socket.close();
socket=null;super.closeServer();
}/*** 打开与FTP服务器的连接
*@params FTP服务器地址
*@parami FTP服务器端口
*@throwsIOException*/publicvoidopenServer(String s,inti)throwsIOException{super.openServer(s,i);
serverAddr=s;
}
}/*** 自定义的FTP异常类*/classMyFtpProtocolExceptionextendsIOException{
MyFtpProtocolException(String s){super(s);
}
}