packagecom.kexin.common.util;importcom.kexin.admin.entity.ftp.Ftp;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importorg.apache.log4j.Logger;import java.io.*;public classFtpUtil {private static Logger logger = Logger.getLogger(FtpUtil.class);private staticFTPClient ftp;/*** 获取ftp连接
*
*@paramf
*@return*@throwsException*/
public static boolean connectFtp(Ftp f) throwsException {
ftp= newFTPClient();boolean flag = false;intreply;if (f.getPort() == null) {
ftp.connect(f.getIpAddr(),21);
}else{
ftp.connect(f.getIpAddr(), f.getPort());
}
ftp.login(f.getUserName(), f.getPwd());
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply=ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();returnflag;
}
ftp.changeWorkingDirectory(f.getPath());
flag= true;returnflag;
}/*** 关闭ftp连接*/
public static voidcloseFtp() {if (ftp != null &&ftp.isConnected()) {try{
ftp.logout();
ftp.disconnect();
}catch(IOException e) {
e.printStackTrace();
}
}
}/*** ftp上传文件
*
*@paramf
*@throwsException*/
public static void upload(File f) throwsException {
ftp.setControlEncoding("GBK");if(f.isDirectory()) {
ftp.makeDirectory(f.getName());
ftp.changeWorkingDirectory(f.getName());//ftp.setControlEncoding("utf-8");
String[] files =f.list();for(String fstr : files) {
File file1= new File(f.getPath() + "/" +fstr);if(file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2= new File(f.getPath() + "/" +fstr);
FileInputStream input= newFileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
File file2= newFile(f.getPath());
FileInputStream input= newFileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}/*** 下载链接配置
* 本地与,远程地址同步,将所有文件都下载到本地
*@paramf
*@paramlocalBaseDir 本地目录
*@paramremoteBaseDir 远程目录
*@throwsException*/
public static void startDown(Ftp f, String localBaseDir, String remoteBaseDir) throwsException {if(FtpUtil.connectFtp(f)) {try{
FTPFile[] files= null;boolean changedir =ftp.changeWorkingDirectory(remoteBaseDir);if(changedir) {
ftp.setControlEncoding("GBK");//ftp.setControlEncoding("utf-8");
files =ftp.listFiles();for (int i = 0; i < files.length; i++) {try{
downloadFile(files[i], localBaseDir, remoteBaseDir);
}catch(Exception e) {
logger.error(e);
logger.error("下载失败");
}
}
}
}catch(Exception e) {
logger.error(e);
logger.error("下载过程中出现异常");
}
}else{
logger.error("链接失败!");
}
}/*** 下载FTP文件
* 当你需要下载FTP文件的时候,调用此方法
* 根据获取的文件名,本地地址,远程地址进行下载
*
*@paramftpFile
*@paramrelativeLocalPath
*@paramrelativeRemotePath*/
private static voiddownloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {if(ftpFile.isFile()) {if (ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream= null;try{
File locaFile= new File(relativeLocalPath +ftpFile.getName());//判断文件是否存在,存在则返回
if(locaFile.exists()) {return;
}else{
outputStream= new FileOutputStream(relativeLocalPath +ftpFile.getName());
ftp.setControlEncoding("GBK");
ftp.retrieveFile(ftpFile.getName(), outputStream);
outputStream.flush();
outputStream.close();
}
}catch(Exception e) {
logger.error(e);
}finally{try{if (outputStream != null) {
outputStream.close();
}
}catch(IOException e) {
logger.error("输出文件流异常");
}
}
}
}else{
String newlocalRelatePath= relativeLocalPath +ftpFile.getName();
String newRemote= new String(relativeRemotePath +ftpFile.getName().toString());
File fl= newFile(newlocalRelatePath);if (!fl.exists()) {
fl.mkdirs();
}try{
newlocalRelatePath= newlocalRelatePath + '/';
newRemote= newRemote + "/";
String currentWorkDir=ftpFile.getName().toString();boolean changedir =ftp.changeWorkingDirectory(currentWorkDir);if(changedir) {
FTPFile[] files= null;
files=ftp.listFiles();for (int i = 0; i < files.length; i++) {
downloadFile(files[i], newlocalRelatePath, newRemote);
}
}if(changedir) {
ftp.changeToParentDirectory();
}
}catch(Exception e) {
logger.error(e);
}
}
}
}