成都创新互联是一家集网站建设,平顺企业网站建设,平顺品牌网站建设,网站定制,平顺网站建设报价,网络营销,网络优化,平顺网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
微信公共平台开发者接口 WeChatFilter com.gson.WeChatFilter WeChatFilter /wechat/* index.jsp
#p#
- package com.gson;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Date;
- import java.util.Properties;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletInputStream;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.log4j.Logger;
- import com.gson.bean.Articles;
- import com.gson.bean.InMessage;
- import com.gson.bean.OutMessage;
- import com.gson.inf.MessageProcessingHandler;
- import com.gson.util.Tools;
- import com.gson.util.XStreamFactory;
- import com.thoughtworks.xstream.XStream;
- /**
- * 请求拦截
- * @author GodSon
- *
- */
- public class WeChatFilter implements Filter {
- private final Logger logger = Logger.getLogger(WeChatFilter.class);
- private String _token;
- private String conf = "classPath:wechat.properties";
- private String defaultHandler = "com.gson.inf.DefaultMessageProcessingHandlerImpl";
- private Properties p;
- @Override
- public void destroy() {
- logger.info("WeChatFilter已经销毁");
- }
- @Override
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
- Boolean isGet = request.getMethod().equals("GET");
- String path = request.getServletPath();
- String pathInfo = path.substring(path.lastIndexOf("/"));
- if (pathInfo == null) {
- response.getWriter().write("error");
- } else {
- _token = pathInfo.substring(1);
- if (isGet) {
- doGet(request, response);
- } else {
- doPost(request, response);
- }
- }
- }
- private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
- response.setCharacterEncoding("UTF-8");
- response.setContentType("text/xml");
- OutMessage oms = new OutMessage();
- ServletInputStream in = request.getInputStream();
- //转换微信post过来的xml内容
- XStream xs = XStreamFactory.init(false);
- xs.alias("xml", InMessage.class);
- String xmlMsg = Tools.inputStream2String(in);
- InMessage msg = (InMessage) xs.fromXML(xmlMsg);
- //获取自定消息处理器,如果自定义处理器则使用默认处理器。
- String handler = p.getProperty("MessageProcessingHandlerImpl");
- if(handler== null)
- handler = defaultHandler;
- try {
- //加载处理器
- Class> clazz = Class.forName(handler);
- MessageProcessingHandler processingHandler = (MessageProcessingHandler) clazz.newInstance();
- //取得消息类型
- String type = msg.getMsgType();
- //针对不同类型消息进行处理
- if (type.equals(MessageProcessingHandler.MSG_TYPE_TEXT)) {
- oms = processingHandler.textTypeMsg(msg);
- } else if (type.equals(MessageProcessingHandler.MSG_TYPE_LOCATION)) {
- oms = processingHandler.locationTypeMsg(msg);
- } else if (type.equals(MessageProcessingHandler.MSG_TYPE_LINK)) {
- oms = processingHandler.linkTypeMsg(msg);
- } else if (type.equals(MessageProcessingHandler.MSG_TYPE_IMAGE)) {
- oms = processingHandler.imageTypeMsg(msg);
- } else if (type.equals(MessageProcessingHandler.MSG_TYPE_EVENT)) {
- oms = processingHandler.eventTypeMsg(msg);
- }
- if(oms == null){
- oms = new OutMessage();
- oms.setContent("系统错误!");
- }
- //设置发送信息
- oms.setCreateTime(new Date().getTime());
- oms.setToUserName(msg.getFromUserName());
- oms.setFromUserName(msg.getToUserName());
- } catch (ClassNotFoundException e) {
- logger.error("没有找到" + handler + "类", e);
- oms.setContent("系统错误!");
- } catch (InstantiationException e) {
- logger.error(e);
- oms.setContent("系统错误!");
- } catch (IllegalAccessException e) {
- logger.error(e);
- oms.setContent("系统错误!");
- }
- //把发送发送对象转换为xml输出
- xs = XStreamFactory.init(false);
- xs.alias("xml", OutMessage.class);
- xs.alias("item", Articles.class);
- xs.toXML(oms, response.getWriter());
- }
- private void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
- String signature = request.getParameter("signature");// 微信加密签名
- String timestamp = request.getParameter("timestamp");// 时间戳
- String nonce = request.getParameter("nonce");// 随机数
- String echostr = request.getParameter("echostr");//
- //验证
- if (Tools.checkSignature(_token, signature, timestamp, nonce)) {
- response.getWriter().write(echostr);
- }
- }
- /**
- * 启动的时候加载wechat.properties配置
- * 可以在过滤器配置wechat.properties路径
- */
- @Override
- public void init(FilterConfig config) throws ServletException {
- String cf = config.getInitParameter("conf");
- if (cf != null) {
- conf = cf;
- }
- String classPath = this.getClass().getResource("/").getPath().replaceAll("%20", " ");
- conf = conf.replace("classPath:", classPath);
- p = new Properties();
- File pfile = new File(conf);
- if (pfile.exists()) {
- try {
- p.load(new FileInputStream(pfile));
- } catch (FileNotFoundException e) {
- logger.error("未找到wechat.properties", e);
- } catch (IOException e) {
- logger.error("wechat.properties读取异常", e);
- }
- }
- logger.info("WeChatFilter已经启动!");
- }
- }
名称栏目:Java:实现微信公众平台
网站URL:http://www.36103.cn/qtweb/news15/21165.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联