当前位置:首页 » JAVA技术教程

微信公众平台java图文

2013-09-22 19:33 本站整理 浏览(4)

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

public class HelloWorldServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		 resp.setContentType("text/html;charset=UTF-8");
	        PrintWriter pw = resp.getWriter();
	        String echo = req.getParameter("echostr");
	        echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8");
	        pw.println(echo);
			
	}
   
	
	public void	doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		ServletInputStream weixinstr = req.getInputStream();
		SAXReader Reader = new SAXReader();
		try {
			Document document = Reader.read(weixinstr);
			String fromusername=document.selectSingleNode("//FromUserName").getText();
			String tousername=document.selectSingleNode("//ToUserName").getText();
			String msgtype=document.selectSingleNode("//MsgType").getText();
			String createtime=document.selectSingleNode("//CreateTime").getText();
           String reply = null ;
			if (msgtype.equals("location")){
            String latitude=document.selectSingleNode("//Location_X").getText();
            String longitude=document.selectSingleNode("//Location_Y").getText();
				reply="你的是纬度"+latitude+"经度是"+longitude;
             	}
          else if (msgtype.equals("image")){
          reply="你发的是图片";
          }
          else if(msgtype.equals("text")){
           String content=document.selectSingleNode("//Content").getText();
            if (content.equals("1")){
				reply="公司简介";
             	}
			else if (content.equals("2")){
				reply="最新产品";
             	}
            else {
            reply="默认回复";
            }
          }
          else if(msgtype.equals("event")){ 
           String event=document.selectSingleNode("//Event").getText();
            if (event.equals("subscribe")){
            reply="感谢你的关注";
            }
            }
          else{
          reply="功能未开发";
          } 
			StringBuffer respMessage = new StringBuffer();
			respMessage.append(("<xml><ToUserName><![CDATA["+fromusername+"]]></ToUserName><FromUserName><![CDATA["+tousername+"]]></FromUserName><CreateTime><![CDATA["+createtime+"]]></CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA["+reply+"]]></Title><Description><![CDATA[图文信息]]></Description><PicUrl><![CDATA[http://res.wx.qq.com/mpres/htmledition/images/login/logo.png]]></PicUrl><Url><![CDATA[http://www.baidu.com]]></Url></item></Articles><FuncFlag>0</FuncFlag></xml>"));
			PrintWriter pw = resp.getWriter();
			pw.println(respMessage);   
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}}
微信java开发群:196673544