FTP文件的下载
官方文档:http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/package-summary.html
package com.iengine.data.filter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class LogFilter implements Filter{
/**
* 从187,195服务器上拉日志文件
*/
public void init(FilterConfig filterConfig) throws ServletException{
String path187 = "E:\\www.360iii.com\\WebLog\\log187\\";
String path195 = "E:\\www.360iii.com\\WebLog\\log195\\";
FTPClient client = new FTPClient();
client.setControlEncoding("UTF-8");
try {
/**
* 连接187服务器
*/
client.connect("ip");
client.login("user", "password");
System.out.println("ip:Login\t FTP Statu:" + client.getReplyCode() + "\tFTP Success:" + client.getReplyString());
FTPFile[] files187 = client.listFiles("");
System.out.println(files187.length);
byte filesFlag[] = new byte[files187.length];
System.out.println(filesFlag.length);
File directory187 = new File(path187);
if(directory187.isDirectory()){
File[] localFiles187 = directory187.listFiles();
// for(int i = 0; i < localFiles187.length; i ++){
// System.out.println("本地文件:" + i + "---" + localFiles187[i].getName());
// }
System.out.println("localFiles187 size:" + localFiles187.length);
for(int i = 0; i < files187.length; i ++ ){
// System.out.println(files187[i].getName());
for(int j = 0; j < localFiles187.length; j ++ ){
// System.out.println(localFiles187[j].getName());
if(localFiles187[j].getName().equals(files187[i].getName())){
filesFlag[i] = 1;
// System.out.println("标记位置:" + filesFlag[i]);
// System.out.println(files187[i].getName() + "在本地存在,标记不下载!");
break;
}
}
}
for(int i = 0; i < files187.length; i ++ ){
if(filesFlag[i] == 0){
File log = new File(path187 + files187[i].getName());
boolean flag = client.retrieveFile(files187[i].getName(), new FileOutputStream(log));
System.out.println("ip Log Download:" + log.getName() + "\t Success:" + flag + "\t FTP Statu:" + client.getReplyCode() + "\t FTP Connetion:" + client.getReplyString());
}
}
}
client.disconnect();
System.out.println("ip Download Complete! Exit 187 " + client.getReplyCode() + " " + client.getReplyString());
/**
* 连接195服务器
*/
client.connect("ip");
client.login("user", "password");
System.out.println("ip:Login\t FTP Statu:" + client.getReplyCode() + "\tFTP Success:" + client.getReplyString());
System.out.println();
FTPFile[] files195 = client.listFiles("");
byte filesFlag195[] = new byte[files195.length];
File directory195 = new File(path195);
if(directory195.isDirectory()){
File[] localFiles195 = directory195.listFiles();
// for(int i = 0; i < localFiles195.length; i ++){
// System.out.println(localFiles195[i].getName());
// }
for(int i = 0; i < files195.length; i ++ ){
// System.out.println(files195[i].getName());
for(int j = 0; j < localFiles195.length; j ++ ){
// System.out.println(localFiles195[j].getName());
if(localFiles195[j].getName().equals(files195[i].getName())){
filesFlag195[i] = 1;
// System.out.println(filesFlag195[i]);
// System.out.println(files195[i].getName() + "在本地存在,标记不下载!");
break;
}
}
}
for(int i = 0; i < files195.length; i ++ ){
if(filesFlag195[i] == 0){
File log = new File(path195 + files195[i].getName());
boolean flag = client.retrieveFile(files195[i].getName(), new FileOutputStream(log));
System.out.println("ip Log Download:" + log.getName() + "\t Success:" + flag + "\t FTP Statu:" + client.getReplyCode() + "\t FTP Connetion:" + client.getReplyString());
}
}
}
client.disconnect();
System.out.println("ip Download Complete! Exit 195" + client.getReplyCode() + " " + client.getReplyString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重写Filter接口的 destroy()方法
*/
public void destroy() {
}
/**
* 重写Filter接口的 doFilter(ServletRequest request, ServletResponse response,FilterChain chain)方法
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
}
}
注:在项目布署时,检测本地文件是否和服务器上的文件相同,如果有不同的文件就从服务器上拉文件
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步