-
微信公众平台开发模式(JAVA) SDK/微信app
【核心代码】package com.gson;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.Method;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.bean.TextOutMessage;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); logger.debug("输入消息:[" xmlMsg "]"); 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(); Method mt = clazz.getMethod(type "TypeMsg", InMessage.class); oms = (OutMessage) mt.invoke(processingHandler, msg); if (oms == null) { oms = new TextOutMessage(); ((TextOutMessage) oms).setContent("系统错误!"); } setMsgInfo(oms,msg); } catch (Exception e) { logger.error(e); oms = new TextOutMessage(); ((TextOutMessage) oms).setContent("系统错误!"); try { setMsgInfo(oms,msg); } catch (Exception e1) { logger.error(e); } } // 把发送发送对象转换为xml输出 xs = XStreamFactory.init(true); xs.alias("xml", oms.getClass()); xs.alias("item", Articles.class); String xml = xs.toXML(oms); logger.debug("输出消息:[" xml "]"); response.getWriter().write(xml); } 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); } } private void setMsgInfo(OutMessage oms,InMessage msg) throws Exception { // 设置发送信息 Class outMsg = oms.getClass().getSuperclass(); Field CreateTime = outMsg.getDeclaredField("CreateTime"); Field ToUserName = outMsg.getDeclaredField("ToUserName"); Field FromUserName = outMsg.getDeclaredField("FromUserName"); ToUserName.setAccessible(true); CreateTime.setAccessible(true); FromUserName.setAccessible(true); CreateTime.set(oms, new Date().getTime()); ToUserName.set(oms, msg.getFromUserName()); FromUserName.set(oms, msg.getToUserName()); } /** * 启动的时候加载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已经启动!"); }}
- 2014-01-21下载
- 积分:1
-
LifeCycle
jsf开发
Web应用程序的开发与传统的单机程序开发在本质上存在着太多的差异,Web应用程序开发人员至今不可避免的必须处理HTTP的细节,而HTTP无状态的(stateless)本质,与传统应用程序必须维持程序运行过程中的信息有明显的违背,再则Web应用程序面对网站上不同的使用者同时的存取,其执行线程安全问题以及数据验证、转换处理等问题,又是复杂且难以解决的。
另一方面,本质上是静态的HTML与本质上是动态的应用程序又是一项违背,这造成不可避免的,处理网页设计的美术人员与程序设计人员,必须被彼此加入至视图组件中的逻辑互相干扰,即便一些视图呈现逻辑以标签的方式呈现,试图展现对网页设计美术人员的亲切,但它终究必须牵涉到相关的流程逻辑。
有很多方案试着解决种种的困境,而各自的着眼点各不相同,有的从程序设计人员的角度来解决,有的从网页设计人员的角度来解决,各种的框架被提出,所造成的是各种不统一的标签与框架,为了促进产能的集成开发环境(IDE)难以整合这些标签与框架,另一方面,开发人员的学习负担也不断的加重,他们必须一人了解多个角色的工作。(jsf development
Web application development with traditional stand-alone program development, in essence there are too many differences, Web application developers staff has inevitably have to deal with the details of HTTP, and HTTP stateless (stateless) the nature of traditional application must maintain significant breaches in the program is running in. Moreover, Web applications face simultaneous access to different users on the website, its execution thread safety issues as well as data validation, conversion process is complex and difficult to solution.
On the other hand, is essentially static HTML and essentially dynamic applications is a violation of which caused inevitable handling web design art programming staff, must each added to the view component logic interfere with each other, even if some view rendering logic to render label, trying to show a web design art staff cordial, but it eventually must be involved in the process logic.
Many programs try to solve all the)
- 2013-03-15 09:22:41下载
- 积分:1
-
Android 蓝牙连接,解决数据报文分段 实例源码
Android 蓝牙连接,解决数据报文分段 实例源码
- 2014-08-29下载
- 积分:1
-
AWS 自动缩放图形用户界面
ASGConsole 提供了用户友好为 AWS 自动缩放服务,AWS 没有目前管理控制台。基本功能包括显示/创建/删除/编辑启动配置组、 自动缩放组和缩放比例政策组。高级的功能是您可以远程输入命令到的实例的自动缩放组中,它支持在同一时间做这到多个实例。此外,它还包括日志功能,您可以检查 ASGLog 目录下的日志。
- 2022-04-24 18:33:33下载
- 积分:1
-
java ast 打印机
java ast 打印机
不难 ^ ___ ^
请使用 eclipse
- 2022-04-29 15:54:00下载
- 积分:1
-
java聊天系统
c/s、tcp/ip、私聊、群聊、在线好友列表、登陆界面。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 2022-06-29 23:46:10下载
- 积分:1
-
HTTPURL Connection POST Request example
这是一个简单的教程演示如何 httpURLConnection 使用 POST 方法向注册更新保存在远程服务器中的数据。
- 2022-02-05 08:32:55下载
- 积分:1
-
mule ESB 1.3.3 核心源码
mule esb 1.3.3版核心模块的源码Mule ESB是一种基于java的、轻量级的企业服务总线和集成平台,她允许开发者快速的、简单的连接应用,并能够实现数据的转换。从2005年发表1.0版本以来,Mule吸引了越来越多的关注者,成为开源ESB中的一支独秀。目前许多公司都使用了Mule,比如沃尔玛,惠普,索尼,Deutsche Bank 以及 CitiBank等公司。
- 2022-01-22 06:50:07下载
- 积分:1
-
jbpm +ssh整合+自定义表单
资源描述jbpm +ssh整合+自定义表单jbpm +ssh整合+自定义表单jbpm +ssh整合+自定义表单
- 2022-07-20 01:13:04下载
- 积分:1
-
数据挖掘技术的 Java 代码
此代码是如何用 Java 代码执行在数据挖掘中的新的程序员培训。这是一个有用的代码。
- 2022-01-30 19:13:41下载
- 积分:1