MessageUtil.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.yihu.jw.util;
  2. import org.dom4j.Document;
  3. import org.dom4j.Element;
  4. import org.dom4j.io.SAXReader;
  5. import javax.servlet.http.HttpServletRequest;
  6. import java.io.InputStream;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * Created by Administrator on 2017/5/23 0023.
  12. */
  13. public class MessageUtil {
  14. /**
  15. * 返回消息类型:文本
  16. */
  17. public static final String RESP_MESSAGE_TYPE_TEXT = "text";
  18. /**
  19. * 返回消息类型:音乐
  20. */
  21. public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
  22. /**
  23. * 返回消息类型:图文
  24. */
  25. public static final String RESP_MESSAGE_TYPE_NEWS = "news";
  26. /**
  27. * 请求消息类型:文本
  28. */
  29. public static final String REQ_MESSAGE_TYPE_TEXT = "text";
  30. /**
  31. * 请求消息类型:图片
  32. */
  33. public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
  34. /**
  35. * 请求消息类型:链接
  36. */
  37. public static final String REQ_MESSAGE_TYPE_LINK = "link";
  38. /**
  39. * 请求消息类型:地理位置
  40. */
  41. public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
  42. /**
  43. * 请求消息类型:音频
  44. */
  45. public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
  46. /**
  47. * 请求消息类型:推送
  48. */
  49. public static final String REQ_MESSAGE_TYPE_EVENT = "event";
  50. /**
  51. * 事件类型:subscribe(订阅)
  52. */
  53. public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
  54. /**
  55. * 事件类型:unsubscribe(取消订阅)
  56. */
  57. public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
  58. /**
  59. * 事件类型:CLICK(自定义菜单点击事件)
  60. */
  61. public static final String EVENT_TYPE_CLICK = "CLICK";
  62. /**
  63. * 解析微信发来的请求(XML)
  64. *
  65. * @param request
  66. * @return
  67. * @throws Exception
  68. */
  69. @SuppressWarnings("unchecked")
  70. public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
  71. // 将解析结果存储在HashMap中
  72. Map<String, String> map = new HashMap<String, String>();
  73. // 从request中取得输入流
  74. InputStream inputStream = request.getInputStream();
  75. // 读取输入流
  76. SAXReader reader = new SAXReader();
  77. Document document = reader.read(inputStream);
  78. // 得到xml根元素
  79. Element root = document.getRootElement();
  80. // 得到根元素的所有子节点
  81. List<Element> elementList = root.elements();
  82. // 遍历所有子节点
  83. for (Element e : elementList)
  84. map.put(e.getName(), e.getText());
  85. // 释放资源
  86. inputStream.close();
  87. inputStream = null;
  88. return map;
  89. }
  90. }