public class BillParser { //各类型账单对应的源账单xml数据文件目录(由营帐系统上传) private static String s[] = {"/portal/shell/billInfo/bill/3GbillXMLView/", "/portal/shell/billInfo/bill/billXMLView/", "/portal/shell/billInfo/bill/WHbillXMLView/" }; private static Map<String, String> m1 = new HashMap<String, String>(); private static Map<String, String> m2 = new HashMap<String, String>(); static { //各类型账单html文件最终生成的路径 m1.put(s[0], "/portal/applications/hkweb/WEB-INF/billfile/"); m1.put(s[1], "/portal/applications/hkweb/WEB-INF/billfile/"); m1.put(s[2], "/portal/applications/hkweb/WEB-INF/billfile/"); //各类型账单对应的xsl样式文件路径 m2.put(s[0], "/portal/shell/billInfo/billxsl/nh/billfile.xsl"); m2.put(s[1], "/portal/shell/billInfo/billxsl/wh-zy/billfile.xsl"); m2.put(s[2], "/portal/shell/billInfo/billxsl/wh-zy/billfile.xsl"); } private static SimpleDateFormat format = new SimpleDateFormat("[yyyy-MM-dd_hh-mm-ss]"); public static void main(String args[]) { Timer t = new Timer(); t.schedule( new TimerTask() { public void run() { /* *不间断地扫描各类型的源账单文件的上传目录,如发现有未解析的xml账单数据,则进行解析,完成后,将该账单文件重命名 *账单文件以yyyymm.xml格式命名 */ FilenameFilter fileFilter = new FilenameFilter(){ public boolean accept(File dir, String name) { return name.matches("20//d{4}//.xml"); } }; for(String billPath : s) { File xmlFiles[] = new File(billPath).listFiles(fileFilter); String toPath = m1.get(billPath); for(File f : xmlFiles) { toPath = toPath + f.getName().substring(0, 6) + "/"; File htmlPath = new File(toPath); //如果是第一次解析生成,则创建目录 if(!(htmlPath.exists())) { htmlPath.mkdirs(); //创建账单目录 log.info("创建目录:" + htmlPath.getAbsolutePath()); } try { //解析 BillParser.getInstance().readXMLWriteSub(f, toPath, m2.get(billPath)); } catch (Exception e) { //记录异常日志到日志文件 log.log(Level.INFO, "文件解析出错:", f.getAbsoluteFile()); logException(e); //重命名发生解析错误的源营帐账单文件 f.renameTo(new File(f.getParent() + "/" + format.format(new Date()) + "[ErrorFile]" + f.getName())); continue; //处理下一个文件 } //重命名被处理过的源营帐账单文件 f.renameTo(new File(f.getParent() + "/" + format.format(new Date()) + f.getName())); //删除随html文件生成的子xml文件,以节省磁盘空间 for(File j : htmlPath.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.matches(".*//.xml"); } } )) { j.delete(); } log.info(f.getAbsolutePath() + "解析成功!"); } } } } , 0, 3000); } //单实例 private static volatile BillParser singleInstance = null; public static BillParser getInstance() { if(singleInstance==null) { synchronized(BillParser.class) { if(singleInstance==null) { singleInstance = new BillParser(); } } } return singleInstance; } private BillParser(){ } //日志记录 private static Logger log = Logger.getLogger(BillParser.class.getName()); private static FileHandler fileHandler = null; private static StringWriter sw = new StringWriter(); private static PrintWriter pw = new PrintWriter(sw); static { log.setLevel(Level.INFO); try { fileHandler = new FileHandler("/portal/shell/billInfo/billParse.log", true); fileHandler.setLevel(Level.INFO); fileHandler.setFormatter(new Formatter() { public String format(LogRecord record) { SimpleDateFormat sd = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]"); String d = sd.format(new Date()); return d + record.getLevel() + ":" + record.getMessage() + "/n"; } }); log.addHandler(fileHandler); } catch (Exception e) { e.printStackTrace(); logException(e); } } private static void logException(Exception e) { e.printStackTrace(pw); log.info(sw.toString()); } }