初识Java微信公众号开发 项目结构及全部代码

项目结构:

 

/Weixin/src/com/imooc/po/TextMessage.java

package com.imooc.po;

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;
	}
	
	
}

 

/Weixin/src/com/imooc/servlet/WeixinServlet.java

package com.imooc.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.imooc.po.TextMessage;
import com.imooc.util.CheckUtil;
import com.imooc.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);
		}
				
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		
		PrintWriter out = resp.getWriter();
		try {
			Map<String,String> map = MessageUtil.xmlToMap(req);
			
			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{
					message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.doubushi());
				}
				
				
				
				System.out.println(message);
			}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());
				}
			}
			
			
			out.print(message);
		} catch (DocumentException e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
		
		
		
	}
	
}

 

/Weixin/src/com/imooc/util/CheckUtil.java

package com.imooc.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 = "imooc";
	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]);
		}
		
		// sha1加密
		String temp = getSha1(content.toString());
		
		return temp.equals(signature);
		
	}
	
	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;
		}
	}
}

 

/Weixin/src/com/imooc/util/MessageUtil.java

package com.imooc.util;


import java.io.IOException;
import java.io.InputStream;
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.imooc.po.TextMessage;
import com.thoughtworks.xstream.XStream;

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";
	
	/**
	 * 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();
		
		//从 request中获取输入流
		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;
	}


	public static String textMessageToXml(TextMessage textMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", textMessage.getClass());
		return xstream.toXML(textMessage);
	}
	
	public static String initText(String toUserName,String fromUserName,String content){
		TextMessage text = new TextMessage();
		text.setFromUserName(toUserName);
		text.setToUserName(fromUserName);
		text.setMsgType(MessageUtil.MESSAGE_TEXT);
		text.setCreateTime(String.valueOf(new Date().getTime()));
		text.setContent(content);
		return textMessageToXml(text);
	}
	
	
	/**
	 * 主菜单
	 * @return
	 */
	public static String menuText(){
		StringBuffer sb = new StringBuffer();
		sb.append("欢迎你的关注,请按照菜单提示进行操作:\n\n");
		sb.append("1.课程介绍\n");
		sb.append("2. 慕课网介绍\n");
		sb.append("回复 ? 调出 此菜单...");
		return sb.toString();
	}
	
	public static String firstMenu(){
		StringBuffer sb = new StringBuffer();
		sb.append("介绍微信公众号开发");
		
		return sb.toString();
	}
	
	public static String secondMenu(){
		StringBuffer sb = new StringBuffer();
		sb.append("222222222");
		
		return sb.toString();
	}

	public static String doubushi(){
		StringBuffer sb = new StringBuffer();
		sb.append("哈哈,都不是");
		
		return sb.toString();
	}
}













 

/Weixin/WebContent/WEB-INF/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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Weixin</display-name>
  
  <servlet>
  	<servlet-name>weixinServlet</servlet-name>
  	<servlet-class>com.imooc.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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了173 篇原创文章 · 获赞 24 · 访问量 12万+
展开阅读全文

微信公众号纯Java开发技术怎样选型?

07-14

1、要求做成jsp 加 js 加 css。 2、只有一台配置为E5-2682 V4问题是只分配了2核的性能 2G内存2M带宽。 3、服务器是windows Server 2008 R2 Enterprise。 4、是微信公众号的网站开发,纯Java设计,前后端都是,老板要求。 5、可以参考公众号老皮匠奢护来开发。 6、微信里 是 用openID 登录,在浏览器里 使用账号密码 和 cookie 登录。 7、老板重点要求考虑到安全性和并发。 8、所有考虑用javascript 或者 框架来做。(问题是2核的U,2G内存,估计硬盘空间也不大,并且只有半个月左右的时间,我打算,先用自己最擅长,最简单的出第一个版本,以后再用什么框架之类,因为如果用框架,有可能出现空间不足,性能不足或者不可预知等难题。而用纯JSP写的,因为业务逻辑和需求不复杂,所以实现反而更快,我也擅长,因为只有我一人独立开发,我不能花费某方面太多时间,研究新技术和处理各种不成熟问题,风险太大了。其实框架相当于全家桶,类似你租的房子,本来就不大,只有10平方不到,如果你还把你老家的所有东西,全家桶都搬到你刚刚租10平方的小屋,这就严重有问题。所以,只能精而简,真正高手,也是自己写框架,自己写反射等实现可重用的办法,针对性的,类似架构师这样。因为哪怕用框架,也要针对性的增删,而且不是你写的框架,很麻烦,可能开始开发,很快,但是后期维护极困难,因为后期维护和增加功能都是我负责的。) 9、时间要求最好半个月内,能出第一版(能跑起来,没大的BUG),以后还可以继续完善。 10、![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059105_19944.jpg) 11、我没用过VUE和NODE之类的前端框架,虽然用过Java后端比如Springboot之类的框架 12、数据库用MYSQL 13、请大家给点建议。如果这样需求和难度,一般多少钱呢?包括前后端,美工,运维半年。 ![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059741_180165.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059747_155043.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059751_886569.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059756_990659.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059761_786695.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059765_909878.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059770_932187.jpg)![图片说明](https://img-ask.csdn.net/upload/201907/14/1563059775_937021.jpg) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览