效果图:
项目结构图:
web.xml:
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_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 8 <!-- 配置 Struts2 的 Filter --> 9 <welcome-file-list> 10 11 <!-- <welcome-file>index.jsp</welcome-file> 欢迎页改为MyTag.jsp --> 12 <welcome-file>MyTag.jsp</welcome-file> 13 </welcome-file-list> 14 <!-- 添加微信公众号 --> 15 <servlet> 16 <servlet-name>wechat</servlet-name> 17 <servlet-class>com.test.javaAPI.wechat.WechatServlet</servlet-class> 18 19 </servlet> 20 21 <servlet-mapping> 22 <servlet-name>wechat</servlet-name> 23 <url-pattern>/wechat.do</url-pattern> 24 </servlet-mapping> 25 26 </web-app>
MyTag.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 5 <script type="text/javascript" 6 src="<%=request.getContextPath()%>/js/MyTag.js"></script> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head> 10 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 12 <title>我的自定义标签</title> 13 </head> 14 15 <body> 16 <br> ------发送微信请求-------- 17 <form></form> 18 <form 19 action="wechat.do?signature=71a42b55fa387d372718c3b70c9e842a8683fad8×tamp=1476369983&nonce=30917439&openid=oAcGbwKCtLQB-BzNJoJ_HeK22j1g&encrypt_type=aes&msg_signature=3e8c67453dfcb1b8e2f0fc51f6c8cb033d393cbe" 20 method="post"> 21 <!-- 用于测试token --> 22 <input type="submit" value="发送微信请求" id="txt_4" /> 23 24 </form> 25 ------------ 26 <br> 27 </body> 28 </html>
CheckUtil.java
package com.test.javaAPI.wechat; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; /** * 加密引用自:http://www.henkuai.com/thread-11456-1-1.html * @author Wei * @time 2016年10月13日 上午9:26:08 */ public class CheckUtil { private static final String token = "WeiyongleWechat"; public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 排序 Arrays.sort(arr); // 生成字符串 StringBuilder content = new StringBuilder(); for (int i = 0; i < arr.length; i++) { content.append(arr[i]); } // sha1加密 String temp = getSHA1String(content.toString()); return temp.equals(signature); // 与微信传递过来的签名进行比较 } private static String getSHA1String(String str) { // return DigestUtils.sha1Hex(data); // 使用commons codec生成sha1字符串 try { MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); // 如果是SHA加密只需要将"SHA-1"改成"SHA"即可 digest.update(str.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexStr = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexStr.append(0); } hexStr.append(shaHex); } return hexStr.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } }
DBConnUtil.java
package com.test.javaAPI.wechat; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import oracle.jdbc.driver.OracleDriver; /** * 数据连接工具类 * @author Wei * @time 2016年9月22日 下午10:20:52 */ public class DBConnUtil { static Connection conn; static String url = "jdbc:oracle:thin:@localhost:1521:orcl2"; static String user = "hr"; static String pwd = "hr"; /** * 链接到本地数据库,账号密码都是hr, * @return * @throws SQLException */ public static Connection getConn() throws SQLException { DriverManager.registerDriver(new OracleDriver()); Connection conn = DriverManager.getConnection(url, user, pwd); System.out.println("DBConnUtil.getConn()....,获取到了链接conn"); return conn; } }
MessagePub.java
package com.test.javaAPI.wechat; /** * 微信公用的消息参数 * * @author Wei * @time 2016年10月14日 上午10:19:36 */ public class MessagePub { private String ToUserName; private String FromUserName; private long CreateTime; private String MsgType; private String MsgId; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public long getCreateTime() { return CreateTime; } public void setCreateTime(long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public String getMsgId() { return MsgId; } public void setMsgId(String msgId) { MsgId = msgId; } }
MessageUtil.java
package com.test.javaAPI.wechat; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; 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; /** * 微信消息工具类 * * @author Wei * @time 2016年10月12日 下午10:45:44 */ public class MessageUtil { /** * xml转换为map * * @param request * @return * @throws IOException * @throws DocumentException */ 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()); } String json = UtilJackson.mapToJsonstr(map); System.out.println("json:" + json); ins.close(); return map; } /** * text转换为 XML * * @param msg * @return */ public static String textMessageToXml(TextMessage msg) { System.out.println(msg + "的全类名:" + msg.getClass().getName()); XStream xstream = new XStream(); // 这里需要替换 ,把<com.test.javaAPI.wechat.TextMessage>这样的标签替换为<xml> return xstream.toXML(msg).replaceAll(msg.getClass().getName(), "xml"); } public static String MessageToXml(Object msg) { System.out.println(msg + "的全类名:" + msg.getClass().getName()); XStream xstream = new XStream(); // 这里需要替换 ,把<com.test.javaAPI.wechat.TextMessage>这样的标签替换为<xml> return xstream.toXML(msg).replaceAll(msg.getClass().getName(), "xml"); } }
PicMessage.java
package com.test.javaAPI.wechat; /** * 图片消息实体类 * * @author Wei * @time 2016年10月14日 上午10:20:10 */ public class PicMessage extends MessagePub { private String PicUrl; private String MediaId; private String ToUserName; public String getPicUrl() { return PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getMediaId() { return MediaId; } public void setMediaId(String mediaId) { MediaId = mediaId; } public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } }
TextMessage.java
package com.test.javaAPI.wechat; public class TextMessage { private String ToUserName; private String FromUserName; private String CreateTime; private String MsgType; private String Content; private String MsgId; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public String getCreateTime() { return CreateTime; } public void setCreateTime(String createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } public String getMsgId() { return MsgId; } public void setMsgId(String msgId) { MsgId = msgId; } }
UtilJackson.java
package com.test.javaAPI.wechat; import java.io.IOException; import java.io.StringWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.io.SegmentedStringWriter; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.junit.Test; import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; /** * json工具类,用于对json相关的一些操作, * * @author Wei * @time 2016年10月2日 下午4:25:25 */ public class UtilJackson { public static String jsonStr = "{"parameters":{"PI_CAE574":"20160908","PI_JSRQ":"20160908","Pi_CAE920":"301","Pi_YAE601":"1","PI_BAE001":"511502","Pi_JFDX":"1","":"pkg_weiyl.getMatchResult"},"serviceId":"directJdbcService","method":"savePointProcedure"}"; /** * 比较复杂的json格式字符串 */ public static String jsonStr_HN = "{"DataPackage":{"JZZL":[{"CY\"BZ":"2","CYQK":"","CYRQ":"20090205","CYZD":"霍乱","CYZD1":"","CYZDGJDM":"A00.901","JZJLH":"136","JZLB":"21","RYRQ":"20090205","RYZD":"霍乱","RYZD1":"","RYZDGJDM":"A00.901","RYZS":"","SSMC":"","YYBH":"1000","ZYH":"","ZYJSLB":"1"},{"CYBZ":"2","CYQK":"","CYRQ":"20090202","CYZD":"霍乱","CYZD1":"","CYZDGJDM":"A00.901","JZJLH":"142","JZLB":"21","RYRQ":"20090201","RYZD":"霍乱","RYZD1":"","RYZDGJDM":"A00.901","RYZS":"","SSMC":"","YYBH":"1000","ZYH":"","ZYJSLB":"1"},{"CYBZ":"2","CYQK":"","CYRQ":"20090304","CYZD":"霍乱","CYZD1":"","CYZDGJDM":"A00.901","JZJLH":"132","JZLB":"21","RYRQ":"20090303","RYZD":"霍乱","RYZD1":"","RYZDGJDM":"A00.901","RYZS":"","SSMC":"","YYBH":"1000","ZYH":"","ZYJSLB":"1"},{"CYBZ":"2","CYQK":"","CYRQ":"20090306","CYZD":"nihao","CYZD1":"","CYZDGJDM":"","JZJLH":"140","JZLB":"21","RYRQ":"20090305","RYZD":"nihao","RYZD1":"","RYZDGJDM":"","RYZS":"","SSMC":"","YYBH":"1000","ZYH":"","ZYJSLB":"1"}]},"FunctionParams":{"FHZ":"1","GET_PAGE_TOTAL":"4","HAS_NEXT_REQUEST":"false","MSG":"执行成功!","SESSIONID":"QKSZPNrRPptTGs3ymqvJhQLZyxKJpd4XCHGJQBGFQcFQtwRYGvxS!306292812!1338878737991"}}"; public static String jsonStr_KEY_DataPackage = "DataPackage"; public static String jsonStr_KEY_FunctionParams = "FunctionParams"; public static String jsonStr4 = "{"verified":false,"name":{"last":"Hankcs","first":"Joe"},"userImage":"Rm9vYmFyIQ==","gender":"MALE"}"; public static String jsonStr4_KEY1 = "verified"; public static ObjectMapper objMap; public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { // ObjectMapper objMap = new ObjectMapper(); // // Map map = objMap.readValue(UtilJackson.jsonStr, Map.class); // Map map = objMap.readValue(UtilJackson.jsonStr_HN, Map.class); // System.out.println(map.toString()); // Set set = map.keySet(); // Iterator it = set.iterator(); // while (it.hasNext()) { // Object key = (Object) it.next(); // Object value = map.get(key); // System.out.println("key:" + key + ",value:" + value); // } // new JacksonTest().removeDataPackage("DataPackage"); String str = removeJsonObjByKey(UtilJackson.jsonStr4, UtilJackson.jsonStr4_KEY1); System.out.println(str); Map map = new HashMap<>(); map.put("xx", "abc"); map.put("xiao", "大大的"); String json = mapToJson(map); System.out.println(json); } /** * 根据键除去json格式字符串中的某一个键值对 * * @param jsonStr * @param key * @return * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public static String removeJsonObjByKey(String jsonStr, String key) throws JsonParseException, JsonMappingException, IOException { ObjectMapper objMap = getObjectMapper(); // 1 把json格式字符串转换为 java.util.Map Map map = objMap.readValue(jsonStr, Map.class); // 2 删除map中的对应key的项目 map.remove(key); // 准备字节流,接收ObjectMapper中写出的输出流 ByteOutputStream bops = new ByteOutputStream(); // 3 把map重新转换为json格式字符串 objMap.writeValue(bops, map); if (!"".equals(bops)) { return bops.toString(); } return ""; } /** * 方法的作用:去除一个json格式字符串的某一个key 删除 这个json字符串里的这个key对应的对象 该方法与框架中的 String * cn.sinobest.framework.web.his.JsonManager.removeDataPackage(String * jsonStr) 这个方法的功能一致 * * @param jsonKey * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ @Test public static String removeDataPackage(String jsonKey) throws JsonParseException, JsonMappingException, IOException { ObjectMapper objMap = getObjectMapper(); Map map = objMap.readValue(UtilJackson.jsonStr_HN, Map.class); // map.remove("DataPackage"); map.remove(jsonKey); ByteOutputStream bops = new ByteOutputStream(); objMap.writeValue(bops, map); System.out.println(bops.toString()); return null; } /** * map转换为json格式字符串 * * @param map * @throws IOException * @throws JsonMappingException * @throws JsonGenerationException */ @Test public static String mapToJson(Map<Object, Object> map) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objMap = getObjectMapper(); return objMap.writeValueAsString(map); } @Test public static String mapToJsonstr(Map<String, String> map) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objMap = getObjectMapper(); return objMap.writeValueAsString(map); } /** * 把list转换为json格式字符串 * @param list * @return * @throws JsonGenerationException * @throws JsonMappingException * @throws IOException */ public static String ListToJson(List list) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objMap = getObjectMapper(); return objMap.writeValueAsString(list); } /** * 一下这段代码是源码 */ /*public String writeValueAsString(Object value) throws IOException, JsonGenerationException, JsonMappingException { // alas, we have to pull the recycler directly here... SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler()); _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value); return sw.getAndClear(); }*/ /** * 传入map或者list对象,转换为字符串 * * @param obj * @return */ public static String getJsonString(Object obj) { ObjectMapper om = getObjectMapper(); StringWriter sw = new StringWriter(); try { JsonGenerator jg = new JsonFactory().createJsonGenerator(sw); om.writeValue(jg, obj); jg.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sw.toString(); } /** * 获取ObjectMapper * * @return */ public static ObjectMapper getObjectMapper() { if (objMap == null) { objMap = new ObjectMapper(); } return objMap; } }
UtilStories.java
package com.test.javaAPI.wechat; /** * 微信的一些操作提示 * * @author Wei * @time 2016年10月14日 下午5:20:59 */ public class UtilStories { public static String STORY00 = "我:大师,帮我看看我和女友的恋情能顺利吗。大师眉头紧皱:这个……我:大师有何难言不妨直说。大师:怒我直言,你这个牌子的充气娃娃容易爆。"; public static String STORY01 = "小李:“我爱上个女护士,非常漂亮,想要追求她,怎么展开?”大黄:“简单,你假装生病,然后接近她,日久生情,肯定能成功!”几天后,小李又来找大黄小李:“你出的什么狗屁主意,昨天我像她表白,她说,别的科室病人可以接受,她科室的病人,她不敢接受!”大黄:“你没问为什么?”小李:“问了,她说,来他们性病科的男人,肯定都不老实!”大黄:“我靠,她是性病科的护士啊!”"; public static String STORY02 = "天上飘着雪花,妻子在屋里补衣服,老公边做木工活儿边问:“雪下多厚啦?”妻子答:“有煎饼那么厚了。”过了一会儿,木匠又问:“雪下多厚啦?”妻子答:“有油馍那么厚了。”又过了一个小时,木匠再次问:“雪下多厚了?”妻答:“有烧饼那么厚了。”木匠怒道:“你就知道吃!”顺手给了妻子一个耳光。妻子指着自己的嘴说:“看你把我打的,嘴肿得跟包子似的。”"; public static String SBCX = "查询格式 sbcx单位编号,现在只是测试, 只能够查询 单位的正常参保的总人数,查询格式 sbcx 单位编号 ,参保人数查询支持模糊查询; 例如 sbcx02123456 ,养老账户余额的查询格式: zhcx身份证号,账户余额不支持模糊查询,因为这样的话可能导致查询超时"; }
WechatServlet.java
package com.test.javaAPI.wechat; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import java.util.Map; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dom4j.DocumentException; /** * 微信公众号的访问地址 * * @author Wei * @time 2016年10月13日 下午9:17:02 */ public class WechatServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/xml;charset=utf-8"); PrintWriter out = resp.getWriter(); System.out.println("WechatServlet querystring:" + req.getQueryString()); String projectName = req.getContextPath(); try { Map<String, String> map = MessageUtil.xmlToMap(req); String toUserName = map.get("ToUserName"); String fromUserName = map.get("FromUserName"); String msgType = map.get("MsgType"); String content = map.get("Content"); String message = null; if ("text".equals(msgType)) { // 对文本消息进行处理 TextMessage text = new TextMessage(); text.setFromUserName(toUserName); // 发送和回复是反向的 text.setToUserName(fromUserName); text.setMsgType("text"); text.setCreateTime(new Date().getTime() + ""); text.setContent("你发送的消息是:" + content); if (!"".equals(content) && content.contains("笑话")) { int i = new Random().nextInt(2); String story = ""; switch (i) { case 0: story = UtilStories.STORY00; break; case 1: story = UtilStories.STORY01; break; case 2: story = UtilStories.STORY02; break; default: break; } text.setContent(story); } if (!"".equals(content) && (content.contains("社保") || content.contains("社会"))) { String story = ""; story = UtilStories.SBCX; text.setContent(story); } /** * 养老参保人查询 */ if (!"".equals(content) && (content.contains("sbcx") && (content.length() > 4))) { String cxtj = content.substring(content.indexOf("sbcx") + 4); cxtj = cxtj.trim(); String querySql = "select count(distinct(aac001)) CNT from ac02@TO_MSSCK where aac008 = '1' and aae140 = '110' and aab001 in (select aab001 from ae01@TO_MSSCK where aab999 like '%" + cxtj + "%')"; System.out.println("养老参保人数 查询语句打印:" + querySql); Connection conn = DBConnUtil.getConn(); PreparedStatement prep = conn.prepareStatement(querySql); ResultSet rst = prep.executeQuery(); if (rst.next()) { int num = rst.getInt("CNT"); text.setContent("单位 " + cxtj + "共有正常参保人数 :" + num); } else { text.setContent("对不起,我没查到任何信息,可能是我报错了!"); } } /** * 养老个人账户查询 */ if (!"".equals(content) && (content.contains("zhcx") && (content.length() > 4))) { String cxtj = content.substring(content.indexOf("zhcx") + 4); cxtj = cxtj.trim(); String querySql = "select cac047 from sic81@TO_MSSCK where aac001 in (select aac001 from ac01@TO_MSSCK where aac002 ='" + cxtj + "')"; System.out.println("养老账户 查询语句打印:" + querySql); Connection conn = DBConnUtil.getConn(); PreparedStatement prep = conn.prepareStatement(querySql); ResultSet rst = prep.executeQuery(); if (rst.next()) { String num = rst.getString("cac047"); text.setContent("您" + cxtj + "的养老账户余额 :" + num); } else { text.setContent("对不起,我没查关于您" + cxtj + "的养老账户余额!"); } } message = MessageUtil.textMessageToXml(text); } else if ("image".equals(msgType)) { PicMessage img = new PicMessage(); img.setToUserName(fromUserName); img.setFromUserName(toUserName); img.setCreateTime(new Date().getTime()); img.setMsgType(msgType); // img.setPicUrl(projectName+"/myimg/ydd.jpg"); // img.setPicUrl("/myimg/ydd.jpg"); img.setPicUrl("http://s.dgtle.com/forum/201509/25/192432xwkyrwfyk5l5amcs.jpg"); img.setMediaId(map.get("MediaId")); img.setMsgId(map.get("MsgId")); message = MessageUtil.MessageToXml(img); } System.out.println("message: " + message); out.print(message); // 将回应发送给微信服务器 } catch (DocumentException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { out.close(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * 微信公众号的入口地址,在这里 千万注意不能调用父类的 的doGet(req,resp)方法, 否则会报错,400的错误 HTTP * Status 400 - HTTP method GET is not supported by this URL */ // super.doGet(req, resp); String query = req.getQueryString(); System.out.println("query:" + query); String signature = req.getParameter("signature"); String timestamp = req.getParameter("timestamp"); String nonce = req.getParameter("nonce"); String echostr = req.getParameter("echostr"); System.out.println( "signature:" + signature + ",timestamp:" + timestamp + ",nonce:" + nonce + ",echostr:" + echostr); PrintWriter out = resp.getWriter(); if (CheckUtil.checkSignature(signature, timestamp, nonce)) { System.out.println("echostr:" + echostr); // out.print(echostr); // 校验通过,原样返回echostr参数内容 out.write(echostr); } } }