import java.io.*;
import java.util.ArrayList;
import java.util.ListIterator;

/** *//**
* @author Ansty
*
*/
public class LogKiller ...{
private String ip; // 要清除的IP
private ArrayList logArray = new ArrayList(); // IIS日志内容
private ArrayList newLogArray = new ArrayList(); // 处理后的IIS日志内容
private String fileName; // 日志文件地址

/** *//**
* @param fileName
* 准备清理的日志文件
* @param ip
* 准备清理的IP
*/
public LogKiller(String fileName, String ip) ...{
this.ip = ip;
logArray = new ArrayList();
newLogArray = new ArrayList();
this.fileName = fileName;
}

/** *//**
* 快速处理
*/
public void exec() ...{
try ...{
Process p = null;
p = Runtime.getRuntime().exec("cmd /c iisreset /stop");
execResult(p);
p = Runtime.getRuntime().exec("cmd /c net stop w3svc");
execResult(p);
readLog();
execLog();
writeLog(newLogArray);
p = Runtime.getRuntime().exec("cmd /c iisreset /enable");
execResult(p);
p = Runtime.getRuntime().exec("cmd /c iisreset /start");
execResult(p);
p = Runtime.getRuntime().exec("cmd /c net start w3svc");
execResult(p);
} catch (IOException e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

/** *//**
* 设置日志文件地址
* @fileName 日志文件地址
*/
public void setLogFile(String fileName) ...{
this.fileName = fileName;
}

/**//*
* 设置要清理的IP
*/
public void setIP(String ip) ...{
this.ip = ip;
}

/** *//**
* 读日志文件
*/
public void readLog() ...{
try ...{
BufferedReader bFile = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName)));
String data;

while ((data = bFile.readLine()) != null) ...{
logArray.add(data);
}
bFile.close();

} catch (FileNotFoundException e) ...{
// TODO 自动生成 catch 块
System.out.println("文件无法找到");

} catch (IOException e) ...{
e.printStackTrace();
}
}

/** *//**
* 打印日志记录链表
* @param temp 日志记录链表
*/
public void printLog(ArrayList temp) ...{
ListIterator li = temp.listIterator();
while (li.hasNext()) ...{
System.out.println(li.next().toString());
}
}

/** *//**
* 处理日志文件,排除指定IP的日志
*/
public void execLog() ...{
ListIterator li = logArray.listIterator();
String temp;
while (li.hasNext()) ...{
temp = li.next().toString();
try ...{
if (temp.split(" ")[1].toString().equals(ip)) ...{
} else
newLogArray.add(temp);
} catch (Exception e) ...{
e.printStackTrace();
}
}
}

/** *//**
* 修改IIS日志
* @param temp 修改好的日志记录链表
*/
public void writeLog(ArrayList temp) ...{
ListIterator li = temp.listIterator();
try ...{
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
while (li.hasNext()) ...{
bw.write(li.next().toString() + " ");
}
bw.flush();
bw.close();
} catch (IOException e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

/** *//**
* 打印DOS执行命令返回信息
* @param p DOS执行命令process
*/
public void execResult(Process p) ...{
BufferedReader bw = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String temp;
try ...{
while ((temp = bw.readLine()) != null) ...{
System.out.println(temp);
}
} catch (Exception e) ...{
e.printStackTrace();
}
}

public static void main(String args[]) ...{
String logFile;
String ipAddr;
try ...{
System.out.println("输入IIS日志文件完整地址(区分大小写):");
BufferedReader dir = new BufferedReader(new InputStreamReader(
System.in));
logFile = dir.readLine();
System.out
.println("输入要清理的IP(必须完整对应日志中的IP,192.168.1.1不能写成192.168.1.01):");
BufferedReader ip = new BufferedReader(new InputStreamReader(
System.in));
ipAddr = ip.readLine();
LogKiller lk = new LogKiller(logFile, ipAddr);
lk.exec();

} catch (IOException e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
2276

被折叠的 条评论
为什么被折叠?



