使用
FTPClient
/**
连接ftp
*/
public class FtpRemote{
FTPClient ftpClient;
public void connect(){
ftpClient= new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName,
ftpPassword);// 登陆FTP服务器
ftpClient .setType(FTPTransferType.BINARY);//文件传输格式
ftpClient.setConnectMode(connectMode==0? FTPConnectMode.PASV : FTPConnectMode.ACTIVE);//连接模式
}
/**
上传文件到ftp
localFilePath 为本地文件全路径
remoteFileName 为上传到服务器后的文件名称
remotePath 为上传文件的ftp路径
*/
public boolean putFile(String localFilePath, String remoteFileName, String remotePath){
try
{
connect();
ftpClient.chdir(remotePath);///进入到远程文件夹之中
ftpClient.put(localFileName, remoteFileName);
return true;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
catch (FTPException e)
{
e.printStackTrace();
return false;
}
finally{
if (ftpClient!= null) {
ftpClient.quit();
}
}
}
/**
下载文件到本地
localFilePath 为本地文件全路径
remoteFileName 为ftp服务器的文件名称
remotePath 为下载文件的ftp路径
*/
try
{
connect();
ftpClient.chdir(remotePath);//进入到远程文件夹之中
ftpClient.get(localFilePath, remoteFileName);
return true;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
catch (FTPException e)
{
e.printStackTrace();
return false;
}
finally{
if (ftpClient!= null) {
ftpClient.quit();
}
}
}
}