第一个类,读取ip、存入数据到xml文件、操作从xml文件取出来的数据
/** * */ package com.topinfo.hazardMonitor.util; import java.io.FileWriter; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * @author Zahir * */ public class IpAddress { private static String ipAdress=null; private String XMLFile; // private static String xPath=null; private static String value="/list"; @SuppressWarnings("unchecked") public IpAddress(){ // HashMap xmlMap = new HashMap(); // xmlMap.put("mo","http://www.ttt.com/ttt-TrdInfo-1-0"); // xPath="//mo:list/IpAddress"; } @SuppressWarnings("unchecked") public boolean judge(HttpServletRequest request,String filePath) throws IOException { boolean judge=false; XMLFile = filePath; String ip=IpAddress.getIpAddress(request); int num=0; //读取xml List list= ParseXml.getData(XMLFile, value); if(!list.isEmpty() && list !=null){ for(int i=0;i<list.size();i++){ if(ip.equals(list.get(i))){ num++; } } if(num>3){ return false;//不弹出窗口 }else{ IpAddress.IpAddToXml(ip,XMLFile); return true;//弹出窗口 } } IpAddress.IpAddToXml(ip,XMLFile); return true;//弹出窗口 } //获取ip地址 private static String getIpAddress(HttpServletRequest request) { if (request.getHeader("x-forwarded-for") == null) { ipAdress= request.getRemoteAddr(); return ipAdress; } ipAdress= request.getHeader("x-forwarded-for"); return ipAdress; } public static void IpAddToXml(String ipAdress,String path) throws IOException { try { SAXReader reader = new SAXReader(); Document document = reader.read(path); if(document!=null){ Element root = document.getRootElement(); List<Element> elements = root.elements(); int index = 0; for(Element element:elements){ index++; if(ipAdress.equals(element.attributeValue("ip"))){ Element elem = DocumentHelper.createElement("IpAddress"); elem.addAttribute("ip", ipAdress); elements.add(index,elem); break; } } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter(path),format); writer.write(document); writer.close(); System.out.println(); }else{ Document xmlfile=DocumentHelper.createDocument(); Element root = xmlfile.addElement( "list" ); Element element=root.addElement( "IpAddress" ); element.addAttribute("ip", ipAdress); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter(path),format); writer.write(document); writer.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.从xml文件读取数据写在一个单独的类中,以便通用
/** * */ package com.topinfo.hazardMonitor.util; /** * @author Zahir * */ import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * XMLFile--XML文件 * xPath--xpath string * value--attribute name */ public class ParseXml { @SuppressWarnings({ "unchecked", "null" }) public static List getData(String XMLFile,String value) { List result=new ArrayList(); String ipAdress; int i=0; try{ SAXReader reader = new SAXReader(); Document document = reader.read("D:/IpAddress.xml"); Element root = document.getRootElement(); List<Element> elements = root.elements(); for(Element element:elements){ ipAdress= element.attributeValue("ip"); result.add(i++, ipAdress); } } catch (Exception e) { e.printStackTrace(); } return result; } }