import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpManager {
private static FTPClient ftpClient = new FTPClient();
private static boolean isSuccess = false;
/**
* 连接并登录到ftp服务器
* @param ftpUrl:远程ftp地址
* @param port:端口号,默认为21
* @param loginName:登录名称
* @param passWord:登录密码
* @param remotePath:远程目录
* @return
*/
public static boolean connect(String ftpUrl,int port,String loginName,String passWord,String remotePath){
int replyCode = 0;
try {
//设置编码格式,防止文件名称中出现中文乱码
ftpClient.setControlEncoding("UTF-8");
//连接ftp服务器
ftpClient.connect(ftpUrl, port);
replyCode = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(replyCode)){
//验证登录
if(ftpClient.login(loginName, passWord)){
isSuccess = true;
}
}else{
ftpClient.disconnect();
}
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); // 设置上传文件以二进制上传
//切换到ftp服务器当前的工作目录
ftpClient.changeWorkingDirectory(remotePath);
} catch (IOException e) {
return isSuccess;
}
return isSuccess;
}
/**
* 将本地文件上传到远程ftp服务器
* @param ftpUrl:远程ftp地址
* @param port:端口号,默认为21
* @param loginName:登录名称
* @param passWord:登录密码
* @param remotePath:保存上传文件的远程工作目录
* @param localFile:本地要上传的文件,包含路径信息
*/
public static void uploadFile(String ftpUrl,int port,String loginName,String passWord,String remotePath,String localFile){
File remoteFile = null;
//上传的文件名称,不能包含路径信息,否则无法上传成功
String remoteFileName = "";
InputStream inputStream = null;
boolean success = connect(ftpUrl,port,loginName,passWord,remotePath);
if(success){
try {
remoteFile = new File(localFile);
remoteFileName = remoteFile.getName(); // 设置远程文件名
inputStream = new FileInputStream(remoteFile);
//开始上传
ftpClient.storeFile(remoteFileName,inputStream);
inputStream.close();
ftpClient.logout();
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
closeConnect();
}
}else{
}
}
/**
* 从远程ftp服务器上下载文件
* @param ftpUrl:远程ftp地址
* @param port:端口号,默认为21
* @param loginName:登录名称
* @param passWord:登录密码
* @param remotePath:远程工作目录
* @param remoteFileName:要下载的远程文件名,不含路径信息
* @param localPath:保存下载文件的本地目录
*/
public static void downloadFile(String ftpUrl,int port,String loginName,String passWord,String remotePath,String localPath){
File localFile = null;
OutputStream outputStream = null;
boolean success = connect(ftpUrl,port,loginName,passWord,remotePath);
if(success){
try {
//获取当前目录下所有的文件名称,不包含子目录
String[] fileNames = ftpClient.listNames();
for(String fileName:fileNames){
localFile = new File(localPath+File.separator+fileName); // File.separator = "\"
outputStream = new FileOutputStream(localFile);
//开始下载
ftpClient.retrieveFile(fileName,outputStream);
//下载完成后删除该文件
//ftpClient.deleteFile(file);
outputStream.close();
}
ftpClient.logout();
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
closeConnect();
}
}else{
System.out.println("ftp连接失败,请检查用户名和密码是否正确");
}
}
/**
* 关闭与ftp服务器的连接
*/
public static void closeConnect(){
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.disconnect();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if(ftpClient != null && ftpClient.isConnected()){
ftpClient.disconnect();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args){
downloadFile(ip,21,username,password,/images/,E:);
uploadFile(ip,21,username,password,/images/,d:/aa.gif); // 远程路径要写相对
}
}