📄 myftpclient.java
字号:
/**
*本类用于保存ftp客户端以减少ftp客户端连接的次数
*
*/
package msg;
import java.lang.*;
import sun.net.ftp.FtpClient;
import java.util.*;
public class MyFtpClient{
static Hashtable ftpClients=new Hashtable();//ftp客户端
static Hashtable lastTimes=new Hashtable(); //客户端最后更新时间
static long timeOut=5 * 60 * 1000; //5分钟 客户端被关闭的最长等待时间
/**
*constructor 私有化以防止创建实例
*/
private MyFtpClient(){}
/**
*添加新的ftp客户端或更新旧的客户端最后更新时间
*/
static public void setFtpClient(String host,FtpClient ftpClient){
removeTimeOutClient(); //删除超时的ftp客户端
ftpClients.put(host,ftpClient); //保存连接
lastTimes.put(host,new Long(System.currentTimeMillis()) );//保存当前连接时间
}
/**
*获取ftp客户端
*/
static public FtpClient getFtpClient(String host){
lastTimes.put(host,new Long(System.currentTimeMillis()) ); //更新最后连接时间
removeTimeOutClient(); //删除超时的ftp客户端
return (FtpClient)ftpClients.get(host);
}
/**
*删除超时的ftp客户端
*/
static public void removeTimeOutClient(){
//检查所有连接是否超时,若超时就删除/////////////
Enumeration enu=lastTimes.keys();
long l=0;
String host="";
while(enu.hasMoreElements()){
host=(String)enu.nextElement();
l=( (Long)lastTimes.get( host ) ).longValue();
if( System.currentTimeMillis()-l> timeOut){
lastTimes.remove(host);
ftpClients.remove(host);
}
}
///////////////////////////////////////////////
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -