日期:2014-05-17 浏览次数:20742 次
public void saveLogFiles() throws IOException, SQLException, ClassNotFoundException {
Long count = 0L; // 重复记录条数
BufferedReader br = null;
String syslogPath = "D:\\syslog";
File syslog = new File(syslogPath);
Connection con = backUpDao.getConnection();
List<LogInfo> logInfos = new ArrayList<LogInfo>();
for (String dayFileName : syslog.list()) {
String daFilePath = syslogPath + "\\" + dayFileName;
File dayFile = new File(daFilePath);
if (dayFile.isFile()) {
// 如果在syslog文件夹下的当前文件不是日志文件夹,则判断下一个文件
continue;
}
for (String logFileName : dayFile.list()) {
String logFilePath = syslogPath + "\\" + dayFileName + "\\" + logFileName;
System.out.println("正在分析日志文件:" + logFilePath);
File logFile = new File(logFilePath);
try {
br = new BufferedReader(new FileReader(logFile));
String line = "";
while ((line = br.readLine()) != null) {
int indexOfLen = line.indexOf("len");
int indexOfFrom = line.indexOf("from");
if (indexOfLen == -1 || indexOfFrom == -1) {
// 如果找不到对应字符,则跳过该行
continue;
}
String srcIpAndPort = getSrcIpFromLog(line);
String srcIp = srcIpAndPort.split(":")[0];
String destIpAndPort = getDesIpFromLog(line);
String destIp = destIpAndPort.split(":")[0];
String date = getDateFromLog(line, indexOfLen, indexOfFrom);
Calendar cal = Calendar.getInstance();
long startDate = sdf.parse(date).getTime();
cal.setTimeInMillis(startDate);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) +1);
long endDate = cal.getTi