自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java:實(shí)現(xiàn)微信公眾平臺(tái)

開(kāi)發(fā)
本文向大家介紹使用Java來(lái)實(shí)現(xiàn)微信公共平臺(tái)功能,實(shí)現(xiàn)根據(jù)回復(fù)的內(nèi)容返回對(duì)應(yīng)的消息。供大家學(xué)習(xí)使用。

1.  Easyui學(xué)習(xí)班.jpg    

2. [文件] web.xml ~ 755B

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
  5.     id="WebApp_ID" version="3.0"> 
  6.     <display-name>微信公共平臺(tái)開(kāi)發(fā)者接口</display-name> 
  7.     <filter> 
  8.         <filter-name>WeChatFilter</filter-name> 
  9.         <filter-class>com.gson.WeChatFilter</filter-class> 
  10.     </filter> 
  11.     <filter-mapping> 
  12.         <filter-name>WeChatFilter</filter-name> 
  13.         <url-pattern>/wechat/*</url-pattern> 
  14.     </filter-mapping> 
  15.  
  16.     <welcome-file-list> 
  17.         <welcome-file>index.jsp</welcome-file> 
  18.     </welcome-file-list> 
  19. </web-app> 

#p#

3. [文件] WeChatFilter.java

  1. package com.gson; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.FileNotFoundException; 
  6. import java.io.IOException; 
  7. import java.util.Date; 
  8. import java.util.Properties; 
  9.  
  10. import javax.servlet.Filter; 
  11. import javax.servlet.FilterChain; 
  12. import javax.servlet.FilterConfig; 
  13. import javax.servlet.ServletException; 
  14. import javax.servlet.ServletInputStream; 
  15. import javax.servlet.ServletRequest; 
  16. import javax.servlet.ServletResponse; 
  17. import javax.servlet.http.HttpServletRequest; 
  18. import javax.servlet.http.HttpServletResponse; 
  19.  
  20. import org.apache.log4j.Logger; 
  21.  
  22. import com.gson.bean.Articles; 
  23. import com.gson.bean.InMessage; 
  24. import com.gson.bean.OutMessage; 
  25. import com.gson.inf.MessageProcessingHandler; 
  26. import com.gson.util.Tools; 
  27. import com.gson.util.XStreamFactory; 
  28. import com.thoughtworks.xstream.XStream; 
  29.  
  30. /** 
  31.  * 請(qǐng)求攔截 
  32.  * @author GodSon 
  33.  * 
  34.  */ 
  35. public class WeChatFilter implements Filter { 
  36.      
  37.     private final Logger logger = Logger.getLogger(WeChatFilter.class); 
  38.     private String _token; 
  39.     private String conf = "classPath:wechat.properties"
  40.     private String defaultHandler = "com.gson.inf.DefaultMessageProcessingHandlerImpl"
  41.     private Properties p; 
  42.  
  43.     @Override 
  44.     public void destroy() { 
  45.         logger.info("WeChatFilter已經(jīng)銷(xiāo)毀"); 
  46.     } 
  47.  
  48.     @Override 
  49.     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
  50.         HttpServletRequest request = (HttpServletRequest) req; 
  51.         HttpServletResponse response = (HttpServletResponse) res; 
  52.         Boolean isGet = request.getMethod().equals("GET"); 
  53.          
  54.         String path = request.getServletPath(); 
  55.         String pathInfo = path.substring(path.lastIndexOf("/")); 
  56.          
  57.         if (pathInfo == null) { 
  58.             response.getWriter().write("error"); 
  59.         } else { 
  60.             _token = pathInfo.substring(1); 
  61.             if (isGet) { 
  62.                 doGet(request, response); 
  63.             } else { 
  64.                 doPost(request, response); 
  65.             } 
  66.         } 
  67.     } 
  68.  
  69.     private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 
  70.         response.setCharacterEncoding("UTF-8"); 
  71.         response.setContentType("text/xml"); 
  72.          
  73.         OutMessage oms = new OutMessage(); 
  74.         ServletInputStream in = request.getInputStream(); 
  75.         //轉(zhuǎn)換微信post過(guò)來(lái)的xml內(nèi)容 
  76.         XStream xs = XStreamFactory.init(false); 
  77.         xs.alias("xml", InMessage.class); 
  78.         String xmlMsg = Tools.inputStream2String(in); 
  79.         InMessage msg = (InMessage) xs.fromXML(xmlMsg); 
  80.         //獲取自定消息處理器,如果自定義處理器則使用默認(rèn)處理器。 
  81.         String handler = p.getProperty("MessageProcessingHandlerImpl"); 
  82.         if(handler== null
  83.             handler = defaultHandler; 
  84.          
  85.         try { 
  86.             //加載處理器 
  87.             Class<?> clazz = Class.forName(handler); 
  88.             MessageProcessingHandler processingHandler = (MessageProcessingHandler) clazz.newInstance(); 
  89.             //取得消息類(lèi)型 
  90.             String type = msg.getMsgType(); 
  91.             //針對(duì)不同類(lèi)型消息進(jìn)行處理 
  92.             if (type.equals(MessageProcessingHandler.MSG_TYPE_TEXT)) { 
  93.                 oms = processingHandler.textTypeMsg(msg); 
  94.             } else if (type.equals(MessageProcessingHandler.MSG_TYPE_LOCATION)) { 
  95.                 oms = processingHandler.locationTypeMsg(msg); 
  96.             } else if (type.equals(MessageProcessingHandler.MSG_TYPE_LINK)) { 
  97.                 oms = processingHandler.linkTypeMsg(msg); 
  98.             } else if (type.equals(MessageProcessingHandler.MSG_TYPE_IMAGE)) { 
  99.                 oms = processingHandler.imageTypeMsg(msg); 
  100.             } else if (type.equals(MessageProcessingHandler.MSG_TYPE_EVENT)) { 
  101.                 oms = processingHandler.eventTypeMsg(msg); 
  102.             } 
  103.             if(oms == null){ 
  104.                 oms = new OutMessage(); 
  105.                 oms.setContent("系統(tǒng)錯(cuò)誤!"); 
  106.             } 
  107.             //設(shè)置發(fā)送信息 
  108.             oms.setCreateTime(new Date().getTime()); 
  109.             oms.setToUserName(msg.getFromUserName()); 
  110.             oms.setFromUserName(msg.getToUserName()); 
  111.         } catch (ClassNotFoundException e) { 
  112.             logger.error("沒(méi)有找到" + handler + "類(lèi)", e); 
  113.             oms.setContent("系統(tǒng)錯(cuò)誤!"); 
  114.         } catch (InstantiationException e) { 
  115.             logger.error(e); 
  116.             oms.setContent("系統(tǒng)錯(cuò)誤!"); 
  117.         } catch (IllegalAccessException e) { 
  118.             logger.error(e); 
  119.             oms.setContent("系統(tǒng)錯(cuò)誤!"); 
  120.         } 
  121.          
  122.         //把發(fā)送發(fā)送對(duì)象轉(zhuǎn)換為xml輸出 
  123.         xs = XStreamFactory.init(false); 
  124.         xs.alias("xml", OutMessage.class); 
  125.         xs.alias("item", Articles.class); 
  126.         xs.toXML(oms, response.getWriter()); 
  127.     } 
  128.  
  129.     private void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 
  130.         String signature = request.getParameter("signature");// 微信加密簽名 
  131.         String timestamp = request.getParameter("timestamp");// 時(shí)間戳 
  132.         String nonce = request.getParameter("nonce");// 隨機(jī)數(shù) 
  133.         String echostr = request.getParameter("echostr");// 
  134.         //驗(yàn)證 
  135.         if (Tools.checkSignature(_token, signature, timestamp, nonce)) { 
  136.             response.getWriter().write(echostr); 
  137.         } 
  138.     } 
  139.  
  140.     /** 
  141.      * 啟動(dòng)的時(shí)候加載wechat.properties配置 
  142.      * 可以在過(guò)濾器配置wechat.properties路徑 
  143.      */ 
  144.     @Override 
  145.     public void init(FilterConfig config) throws ServletException { 
  146.         String cf = config.getInitParameter("conf"); 
  147.         if (cf != null) { 
  148.             conf = cf; 
  149.         } 
  150.         String classPath = this.getClass().getResource("/").getPath().replaceAll("%20"" "); 
  151.         conf = conf.replace("classPath:", classPath); 
  152.         p = new Properties(); 
  153.         File pfile = new File(conf); 
  154.         if (pfile.exists()) { 
  155.             try { 
  156.                 p.load(new FileInputStream(pfile)); 
  157.             } catch (FileNotFoundException e) { 
  158.                 logger.error("未找到wechat.properties", e); 
  159.             } catch (IOException e) { 
  160.                 logger.error("wechat.properties讀取異常", e); 
  161.             } 
  162.         } 
  163.         logger.info("WeChatFilter已經(jīng)啟動(dòng)!"); 
  164.     } 
  165.  

原文鏈接:http://www.oschina.net/code/snippet_98719_21419

責(zé)任編輯:陳四芳 來(lái)源: 開(kāi)源中國(guó)社區(qū)
相關(guān)推薦

2013-04-15 16:56:48

微信公眾平臺(tái)Android開(kāi)發(fā)

2013-04-08 15:13:39

微信公眾平臺(tái)

2013-04-10 18:45:52

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-08 15:56:49

2013-04-10 13:07:40

微信公眾平臺(tái)Web App

2013-04-10 18:07:08

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-09 17:23:57

微信微信公眾平臺(tái)歡迎信息

2013-04-10 16:15:40

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-10 18:19:40

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-09 23:38:02

微信公眾平臺(tái)開(kāi)發(fā)者

2013-04-10 18:24:48

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-10 18:29:09

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-08 16:19:40

微信微信公眾平臺(tái)圖文消息

2014-11-20 09:38:40

C#

2013-04-08 16:14:10

微信微信公眾平臺(tái)

2013-04-09 18:13:44

微信公眾平臺(tái)關(guān)鍵詞

2013-04-10 17:59:50

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-11 10:50:07

微信公眾平臺(tái)接口開(kāi)發(fā)

2013-04-15 17:18:51

微信公眾平臺(tái)Android開(kāi)發(fā)位置信息識(shí)別

2013-04-01 13:15:49

微信微信公眾賬號(hào)微信推廣
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)