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 java.util.List;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
/**
* ftp工具类
*
* @author alon
*
*/
public class FtpUtil {
private FTPClient ftpClient = null;
private OutputStream outStream = null;
// ftp服务器地址
private String host;
// ftp端口
private int port;
// 连接帐号
private String username;
// 连接密码
private String password;
public FtpUtil(){
super();
ftpClient = new FTPClient();
Properties prop=new Properties();
InputStream in=getClass().getResourceAsStream("/ftp.properties");
try {
prop.load(in);
this.host=prop.getProperty("ftp.host");
String strPort=prop.getProperty("ftp.port");
if(StringUtils.isNotBlank(strPort)){
this.port=Integer.parseInt(strPort);
}
this.username=prop.getProperty("ftp.username");
this.password=prop.getProperty("ftp.password");
} catch (IOException e) {
e.printStackTrace();
}
}
public FtpUtil(String host, int port, String username, String password) {
super();
ftpClient = new FTPClient();
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}
public FtpUtil(String host, String username, String password) {
super();
ftpClient = new FTPClient();
this.host = host;
this.username = username;
this.password = password;
}
/**
* 下载ftp文件
*
* @param remoteDir
* 远程ftp目录
* @param localDir
* 下载到本地目录
* @param fileNames
* 下载文件
*/
public void download(String remoteDir, String localDir, List<String> fileNames) {
try {
// 连接到ftp服务器
connect();
// 下载
if(fileNames!=null&&fileNames.size()>0){
int len=fileNames.size();
for(int i=0;i<len;i++){
String fileName=remoteDir+fileNames.get(i);
File localFile = new File(localDir + fileName);
OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(new String(fileName.getBytes("UTF-8"),
"ISO-8859-1"), is);
is.close();
// 打印
System.out.println(fileName + ":\t下载成功");
}
}
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(outStream);
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
/**
* 上传文件到ftp服务器
*
* @param srcUrl
* 本地上传文件路径
* @param remoteDir
* 远程ftp服务器目录
* @param targetFileName
* 上传到ftp服务器目录文件名
*/
public void upload(String srcUrl, String remoteDir, String targetFileName) {
try {
// 连接到ftp服务器
connect();
// 文件流
File srcFile = new File(srcUrl);
FileInputStream fis = null;
fis = new FileInputStream(srcFile);
// 设置上传目录
ftpClient.changeWorkingDirectory(remoteDir);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
// 设置文件类型
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(targetFileName, fis);
IOUtils.closeQuietly(fis);
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
// 连接到ftp服务器
private void connect() throws Exception {
// 连接到ftp服务器
if (port == 0) {
ftpClient.connect(host);
} else {
ftpClient.connect(host, port);
}
System.out.println("连接到ftp服务器:" + host + "成功,开始登录");
// 登录,用户名,密码
boolean b = ftpClient.login(username, password);
System.out.println("登录成功." + b);
// 检查连接是否成功
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new Exception("ftp服务器拒绝连接");
}
// 设置ftp编码
ftpClient.setControlEncoding("UTF-8");
//ftpClient.setRemoteVerificationEnabled(true);
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
//设置为被动模式
ftpClient.enterLocalPassiveMode();
//二进制文件支持
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
}