项目和软件地址:http://pan.baidu.com/s/1miRNZXe
外网映射工具:ngrok
命令行切换到ngrok目录,我的在D:\ngrok在命令行输入
d:
cd ngrok
ngrok -config ngrok.cfg -subdomain weixincs 8080
weixincs是你要使用的映射网址项目名,重复时记得更改.会等到以下内容
登陆微信公众平台,点击“基本配置”,配置如下内容
代码结构如下:
WeixinServlet代码:
package com.yql.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.yql.po.TextMessage;
import com.yql.util.CheckUtil;
import com.yql.util.MessageUtil;
public class WeixinServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@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);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
Map map = MessageUtil.xmlToMap(request);
String fromUserName = map.get("FromUserName");
String toUserName = map.get("ToUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");
String message = null;
if (MessageUtil.MESSAGE_TEXT.equals(msgType)) {
if ("1".equals(content)) {
message = MessageUtil.initText(toUserName, fromUserName,
MessageUtil.firstMenu());
} else if ("2".equals(content)) {
message = MessageUtil.initText(toUserName, fromUserName,
MessageUtil.secondMenu());
} else if ("?".equals(content) || "?".equals(content)) {
message = MessageUtil.initText(toUserName, fromUserName,
MessageUtil.menuText());
}
} else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) {
String eventType = map.get("Event");
if (MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)) {
message = MessageUtil.initText(toUserName, fromUserName,
MessageUtil.menuText());
}
}
System.out.println(message);
out.print(message);
} catch (DocumentException e) {
e.printStackTrace();
} finally {
out.close();
}
}
}
package com.yql.po;
public class TextMessage {
private String ToUserName;
private String FromUserName;
private long 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 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 getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
}
package com.yql.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class CheckUtil {
private static final String TOKEN ="xxxxx";
public static boolean checkSignature(String signature,String timestamp,String nonce){
String[] arr = new String[]{TOKEN,timestamp,nonce};
//排序
Arrays.sort(arr);
//生成字符串
StringBuffer sb = new StringBuffer();
for(int i=0;i>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}
package com.yql.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
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.yql.po.TextMessage;
public class MessageUtil {
public static final String MESSAGE_TEXT = "text";
public static final String MESSAGE_IMAGE = "image";
public static final String MESSAGE_VOICE = "voice";
public static final String MESSAGE_VIDEO = "video";
public static final String MESSAGE_LINK = "link";
public static final String MESSAGE_LOCATION = "location";
public static final String MESSAGE_EVENT = "event";
public static final String MESSAGE_SUBSCRIBE = "subscribe";
public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
public static final String MESSAGE_CLICK = "CLICK";
public static final String MESSAGE_VIEW = "VIEW";
@SuppressWarnings("unchecked")
public static Map xmlToMap(HttpServletRequest request ) throws IOException, DocumentException{
SAXReader reader = new SAXReader();
Map map = new HashMap();
InputStream input = request.getInputStream();
Document doc = reader.read(input);
List list = new ArrayList();
Element root = doc.getRootElement();
list = root.elements();
for(Element e: list){
map.put(e.getName(), e.getText());
}
input.close();
return map;
}
public static String initText(String toUserName, String fromUserName,
String content) {
TextMessage text = new TextMessage();
text.setFromUserName(toUserName);
text.setToUserName(fromUserName);
text.setMsgType(MESSAGE_TEXT);
text.setCreateTime(new Date().getTime());
text.setContent(content);
return MessageUtil.textMessageToXml(text);
}
public static String textMessageToXml(TextMessage text){
XStream stream = new XStream();
stream.alias("xml", text.getClass());
return stream.toXML(text);
}
/**
* 主菜单
*
* @return
*/
public static String menuText() {
StringBuffer sb = new StringBuffer();
sb.append("欢迎你的关注,请按照菜单提示操作:\n\n");
sb.append("1、近期好看的电影\n");
sb.append("2、87届奥斯卡获奖电影\n");
sb.append("回复?调出帮助菜单");
return sb.toString();
}
/**
* 回复1的菜单
* @return
*/
public static String firstMenu() {
StringBuffer sb = new StringBuffer();
sb.append("近期口碑不错的电影推荐——《大圣归来》,很不错,建议体验一下哦~");
return sb.toString();
}
/**
* 回复2的菜单
* @return
*/
public static String secondMenu() {
StringBuffer sb = new StringBuffer();
sb.append("《消失的爱人》——87届奥斯卡最佳女主角获奖电影,爱情悬疑类型电影迷的最爱。");
return sb.toString();
}
}
weixinServlet
com.yql.servlet.WeixinServlet
weixinServlet
/wx.do