代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
微信接入公共类
作者:
tpythoner
/ 发布于
2015/7/9
/
838
微信后台TONKEN验证以及具体的MESSAGE
package com.wechat.core.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dom4j.DocumentException; import com.wechat.model.TextMessage; import com.wechat.util.CheckUtil; import com.wechat.util.MessageUtil; public class weixinServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取微信传入的四个参数 String signature = req.getParameter("signature"); String timestamp = req.getParameter("timestamp"); String nonce = req.getParameter("nonce"); String echostr = req.getParameter("echostr"); PrintWriter out = resp.getWriter(); if(CheckUtil.checkSignature(signature, timestamp, nonce)){ out.print(echostr); } } @SuppressWarnings("null") @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); PrintWriter out = null; try { Map<String,String> map = MessageUtil.xmlToMap(req); String fromUserName = map.get("FromUserName"); String toUserName = map.get("ToUserName"); String createTime = map.get("CreateTime"); String msgType = map.get("MsgType"); String content = map.get("Content"); /*String msgId = map.get("MsgId");*/ String message = null; if("text".equals(msgType)){ TextMessage text = new TextMessage(); text.setFromUserName(toUserName); text.setToUserName(fromUserName); text.setCreateTime(Long.toString(new Date().getTime())); text.setMsgType("text"); text.setContent("Test"+content); message = MessageUtil.textMessageToXml(text); System.err.println(message); } out.print(message); } catch (DocumentException e) { e.printStackTrace(); }/*finally{ out.close(); } */ } } ]servlet的web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Wechat</display-name> <servlet> <servlet-name>weixinServlet</servlet-name> <servlet-class>com.wechat.core.servlet.weixinServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>weixinServlet</servlet-name> <url-pattern>/wx.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> CheckUtil package com.wechat.util; import java.security.MessageDigest; import java.util.Arrays; public class CheckUtil { private static final String Token = "Token"; public static boolean checkSignature(String signature,String timestamp,String nonce ){ String[] arr = new String[]{Token,timestamp,nonce}; //排序 Arrays.sort(arr); //生成字符串 StringBuffer content = new StringBuffer(); for (int i = 0; i < arr.length; i++) { content.append(arr[i]); } String temp = getSha1(content.toString()); return temp.equals(signature); } //sha1加密 public static String getSha1(String str){ if(str==null||str.length()==0){ return null; } char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length; char buf[] = new char[j*2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0>>>4&0xf]; buf[k++] = hexDigits[byte0&0xf]; } return new String(buf); } catch (Exception e) { return null; } } } MessageUtil package com.wechat.util; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.thoughtworks.xstream.XStream; import com.wechat.model.TextMessage; public class MessageUtil { //xml转化map集合 public static Map<String, String>xmlToMap(HttpServletRequest request) throws IOException, DocumentException{ Map<String,String> map = new HashMap<String, String>(); SAXReader reader = new SAXReader(); InputStream ins = request.getInputStream(); Document doc = reader.read(ins); Element root = doc.getRootElement(); List<Element> list = root.elements(); for (Element e : list) { map.put(e.getName(), e.getText()); } ins.close(); return map; } // 将文本消息对象转化为XML public static String textMessageToXml(TextMessage textMessage){ XStream xstream = new XStream(); xstream.alias("xml",textMessage.getClass()); return xstream.toXML(textMessage); } }
试试其它关键字
微信接入
公共类
微信
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
tpythoner
贡献的其它代码
(
3
)
.
微信接入公共类
.
引用两数交换
.
最大子序列和
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3