package test; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Properties; public class BakFile { private Properties props; private WarningEmail warningEmail; public BakFile(Properties _props) { props = _props; } public void run() { try { // 初始化 this.InitSetup(); // 获取统计信息 this.readResultCount(); } catch (Exception exIO) { warningEmail.send_report_mail("核验备份文件出错啦!!!", exIO.toString()); exIO.printStackTrace(); } } private void InitSetup() throws IOException, SQLException { // 提醒邮件 warningEmail = new WarningEmail(props); } public void readResultCount() { // 获取昨天的日期, Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE,-1);//昨天 String yesterday_ym = new SimpleDateFormat("yyyy-MM").format(cal.getTime()); String yesterday_d = new SimpleDateFormat("dd").format(cal.getTime()); String yesterday = yesterday_ym+"-"+yesterday_d; String path = "/var/jenkins_home/bak_logs/" + yesterday_ym + "/" + yesterday_d; File dirFile = new File(path); //如果dir对应的文件不存在,或者不是一个目录,则退出 if (!dirFile.isDirectory()) { warningEmail.send_report_mail(yesterday+"核验备份文件异常", "未获取到昨天备份目录"); return; } //获取文件夹下所有文件 File[] files = dirFile.listFiles(); File file; List<String> list; String content = ""; String bodyTrContent = ""; if(files!=null && files.length>0) { for (int i = 0; i < files.length; i++) { file = files[i]; if(file.getName().endsWith("count")) { list = readFileContent(file); content = setContent(file, list); bodyTrContent = String.format("%s%s", bodyTrContent, content); } } content = setMailHtml(bodyTrContent, yesterday); warningEmail.send_report_mail(yesterday+"文件备份情况", content); } else { warningEmail.send_report_mail(yesterday+"文件备份为空", "未获取到昨天备份目录"); } } public List<String> readFileContent(File file) { BufferedReader reader = null; FileReader fileReader = null; List<String> list = new ArrayList<String>(); try { fileReader = new FileReader(file); reader = new BufferedReader(fileReader); String tempString = null; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { list.add(tempString); // System.out.println(tempString); } } catch (IOException e) { } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } if(fileReader != null) { try { fileReader.close(); } catch (IOException e1) { } } } return list; } public String setContent(File file, List<String> list) { if(list.size()>=2) { String line1 = list.get(list.size()-2); String line2 = list.get(list.size()-1); return setTableBodyTr(file.getName().replace(".bak.count", ""), line1, line2); } else if(list.size()==1){ // String line1 = list.get(list.size()-1); return setTableBodyTr(file.getName().replace(".bak.count", ""), line1, "未同步"); } else { return setTableBodyTr(file.getName().replace(".bak.count", ""), "未获取到文件夹", "未同步"); } } public String setMailHtml(String content, String yesterday) { String html = String.format("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>" + " <html lang='en'> " + " <head> " + " <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>" + " </head> " + " <body>" + "<div style='font-size:12px;'>%s文件备份情况:<br/><br/>" + "<table width='584' style='border-collapse: collapse;font-size:9pt;width:438pt'><tr style='background-color:#f7f7f7;'>" + "<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px;width:38pt;'>目录</td>" + "<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px;width:60pt;'>本地</td>" + "<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px;width:60pt;'>ftp</td>" + "</tr>", yesterday); html = String.format("%s%s", html, content); html = String.format("%s</table></body></html>", html); return html; } public String setTableBodyTr(String mName, String local, String ftp) { String content = ""; if(!local.equals(ftp)) { ftp = String.format("<span style='color:red;' color='red'>%s</span>", ftp); } content = String.format("%s<tr><td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px 5px 0 5px;width:38pt;'>%s</td>", content, mName); content = String.format("%s<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px 5px 0 5px;width:60pt;'>%s</td>", content, local); content = String.format("%s<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px 5px 0 5px;width:60pt;'>%s</td>", content, ftp); content = String.format("%s</tr>", content); return content; } }