效果圖:javascript
項目結構圖:html
web.xml:java
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:web
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.javasql
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.javajson
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:\n" + 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); } } }