packagecom.echo.services;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;public classFtpOperation {
Logger log= LoggerFactory.getLogger(FtpOperation.class);public static FtpOperation instance = null;privateFtpOperation() {}public staticFtpOperation getInstance(){if( instance == null){synchronized (FtpOperation.class){if (instance == null) {
instance= newFtpOperation();
}
}
}returninstance;
}public voiddownload(FTPClient ftpClient, String remote, String loc ){try{
log.info("remote :"+remote);
ftpClient.changeWorkingDirectory(remote);//生成本地目录
File locFile = newFile(loc);if( !locFile.exists() ){
locFile.mkdir();
}
downLoadFile(ftpClient, loc);
}catch(IOException e) {
e.printStackTrace();
}
}private voiddownLoadFile( FTPClient ftpClient, String LocDir ){try{for(FTPFile ftpFile : ftpClient.listFiles()) {
String path= LocDir+"/"+ftpFile.getName();if( ftpFile.isDirectory() ){//生成本地目录
File localFile = newFile( path );if( !localFile.exists() ){
localFile.mkdir();
}//浏览Ftp目录
ftpClient.changeWorkingDirectory( ftpFile.getName());
downLoadFile(ftpClient, path );
ftpClient.changeToParentDirectory();
}else{if(
(!ftpFile.getName().equals("ualice.access.log") )
){continue;
}
FileOutputStream fostream= newFileOutputStream( path );
ftpClient.retrieveFile(ftpFile.getName(), fostream);
fostream.flush();
fostream.close();
log.info("文件:"+LocDir+ftpFile.getName()+" 下载完毕。");
}
}
}catch(IOException e) {
e.printStackTrace();
}
}
}