📄 ftpfactory.java
字号:
package djr.ftp;
import java.io.IOException;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
public class FtpFactory
{
// 默认远程主机地址
public static final String CST_FTP_HOST = "127.0.0.1";
// 默认用户名
public static final String CST_FTP_USER = "dujiarong";
// 默认密码
public static final String CST_FTP_PASSWORD = "1";
// 第三方FTP客户端类
private FTPClient ftpClient;
// 远程FTP主机地址
private String ftpHost;
// 用户名
private String ftpUser;
// 密码
private String ftpPassword;
public FtpFactory()
{
this(CST_FTP_HOST, CST_FTP_USER, CST_FTP_PASSWORD);
}
public FtpFactory(String ftpHost, String ftpUser, String ftpPassword)
{
this.ftpHost = ftpHost;
this.ftpUser = ftpUser;
this.ftpPassword = ftpPassword;
}
public FTPClient getFtpClient()
{
return ftpClient;
}
// 取得连接到远程主机的FTPClient对象
public FTPClient getLoginedFtpClient() throws IOException, FTPException
{
if (this.ftpClient == null)
{ // 对象未创建
init();
}
if (this.ftpClient.connected() == false)
{ // 对象未连接
this.ftpClient.setRemoteHost(this.ftpHost);
this.ftpClient.connect();
this.ftpClient.login(this.ftpUser, this.ftpPassword);
}
return ftpClient;
}
// 关闭FTP连接
public static void closeFtpClient(FTPClient ftpClient) throws IOException, FTPException
{
if (ftpClient != null || ftpClient.connected())
{// 处于连接状态
ftpClient.quit();
ftpClient = null;
}
}
// 初始化FTPClient
private void init() throws IOException, FTPException
{
if (this.ftpClient == null)
{
this.ftpClient = new FTPClient();
this.ftpClient.setConnectMode(FTPConnectMode.PASV);
}
}
public void setFtpClient(FTPClient ftpClient)
{
this.ftpClient = ftpClient;
}
public String getFtpHost()
{
return ftpHost;
}
public void setFtpHost(String ftpHost)
{
this.ftpHost = ftpHost;
}
public String getFtpPassword()
{
return ftpPassword;
}
public void setFtpPassword(String ftpPassword)
{
this.ftpPassword = ftpPassword;
}
public String getFtpUser()
{
return ftpUser;
}
public void setFtpUser(String ftpUser)
{
this.ftpUser = ftpUser;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -