1 package cn.liuning; 2 3 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.PrintWriter; 7 import java.text.SimpleDateFormat; 8 import java.util.LinkedHashMap; 9 import java.util.List; 10 import java.util.Map; 11 12 import javax.servlet.ServletException; 13 import javax.servlet.http.HttpServlet; 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 17 import org.dom4j.Document; 18 import org.dom4j.Element; 19 import org.dom4j.io.SAXReader; 20 import org.kohsuke.rngom.digested.Main; 21 22 23 public class CoreServe extends HttpServlet 24 { 25 26 //用于存放解析的结果 27 public static Map<String, String> parseXml(HttpServletRequest request, HttpServletResponse response) throws Exception 28 { 29 // 将解析结果存储在HashMap中 ,用到了泛型,就是Map中只能使用string-string的对用,其他数据类型不行 30 Map<String, String> map = new LinkedHashMap<String, String>(); 31 // 从request中取得输入流 32 InputStream inputStream = request.getInputStream(); 33 // 读取输入流 34 SAXReader reader = new SAXReader(); 35 Document document = reader.read(inputStream); 36 // 得到xml根元素 37 Element root = document.getRootElement(); 38 // 得到根元素的所有子节点 39 List<Element> elementList = root.elements(); 40 // 遍历所有子节点,增强for进行循环 41 //也可以使用传统方式 42 for (Element e : elementList) 43 map.put(e.getName(), e.getText()); 44 // 释放资源 45 inputStream.close(); 46 inputStream = null; 47 48 49 //原样输出<XML>文档 50 String value = document.asXML(); 51 char []str = value.toCharArray(); 52 PrintWriter out = response.getWriter(); 53 out.println("原始消息"); 54 // out.println(value); 55 //直接输出string会有<?xml version="1.0" encoding="UTF-8"?> 56 //所以除去头上的39个元素输出 57 for(int i=0;i<value.length()-39;i++) 58 { 59 out.print(str[i+38]); 60 } 61 return map; 62 } 63 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 64 { 65 try { 66 //解决乱码问题 67 request.setCharacterEncoding("UTF-8"); 68 response.setContentType("text/html;charset=UTF-8"); 69 70 Map<String, String> requestMap; 71 requestMap = parseXml(request,response); 72 String fromUserName = requestMap.get("FromUserName"); 73 String toUserName = requestMap.get("ToUserName"); 74 String createtime = requestMap.get("CreateTime"); 75 int ctime = Integer.parseInt(createtime); 76 String msgType = requestMap.get("MsgType"); 77 PrintWriter out = response.getWriter(); 78 out.println("解析结果:"); 79 out.print("FromUserName(来自):");out.println(fromUserName); 80 out.print("ToUserName(发送至):");out.println(toUserName); 81 out.print("CreateTime(时间):"); 82 83 //时间转换 84 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); 85 out.println(format.format((long)ctime*1000L)); 86 87 out.print("MsgType(类型):");out.println(msgType); 88 if(msgType.equals("text")) 89 { 90 String content = requestMap.get("Content"); 91 String msgId = requestMap.get("MsgId"); 92 out.print("Content(文本内容):");out.println(content); 93 out.print("MsgId(文本消息标记号):");out.println(msgId); 94 } 95 else if(msgType.equals("event")) 96 { 97 String event = requestMap.get("Event"); 98 String eventKey = requestMap.get("EventKey"); 99 out.print("Event(事件):");out.println(event); 100 out.print("Eventkey(事件值):");out.println(eventKey); 101 } 102 out.flush(); 103 out.close(); 104 } catch (Exception e) { 105 // TODO 自动生成的 catch 块 106 e.printStackTrace(); 107 } 108 } 109 }
时间: 2024-12-28 01:12:15