快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

葡京娱乐场的官网_龟发之家论坛



package com.chaowen;

import java.io.IOException;

import java.io.InputStream;import java.io.PrintWriter;

import java.io.StringReader;import java.io.Writer;

import java.net.MalformedURLException;import java.security.MessageDigest;

import java.sec葡京娱乐场的官网urity.NoSuchAlgorithmException;import java.util.Arrays;

import java.util.Date;import java.util.Scanner;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

import com.chaowen.bean.WeChatReqBean;import com.chaowen.bean.WeChatRespBean;

import com.chaowen.common.Constants;import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

/**

* Servlet implementation class WeiXinServlet*/

public class WeiXin葡京娱乐场的官网Servlet extends HttpServlet {private static final long serialVersionUID = 1L;

public static final String Token = "token";

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding("UTF-8");

PrintWriterout=response.getWriter();

String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");

String nonce = request.getParameter("nonce");String[] ArrTmp = { Token, timestamp, nonce };

Arrays.sort(ArrTmp);StringBuffer sb = new StringBuffer();

for (int i = 0; isb.append(ArrTmp[i]);

}String pwd = Encrypt(sb.toString());

String echostr = request.getParameter("echostr");System.out.println("pwd=="+pwd);

System.out.println("echostr=="+echostr);if(pwd.equals(signature)){

if(!"".equals(echostr) && echostr != null){response.getWriter().print(echostr);

}}

out.flush();

out.close();//this.doPost(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Scanner scanner = new Scanner(request.getInputStream());response.setContentType("application/xml");

response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();

try {// 1、获取用户发送的信息

InputStream is = request.getInputStream();

// 取HTTP哀求流长度int size = request.getContentLength();

// 用于缓存每次读取的数据byte[] buffer = 葡京娱乐场的官网new byte[size];

// 用于寄放结果的数组byte[] xmldataByte = new byte[size];

int count = 0;int rbyte = 0;

// 轮回读取while (count

// 每次实际读取长度存于rbyte中rbyte = is.read(buffer);

for(int i=0;ixmldataByte[count + i] = buffer[i];

}count += rbyte;

}is.close();

String requestStr = new String(xmldataByte, "UTF-8");

// 2、解析用户的信息

JAXBContext jc = JAXBContext.newInstance(WeChatReqBean.class);Unmarshaller u = jc.createUnmarshaller();

WeChatReqBean reqBean = (WeChatReqBean) u.unmarshal(new StringReader(requestStr));

// 3、鉴定用户懂得动作String content = getContent(reqBean);

// 4、创建一个回覆消息jc = JAXBContext.newInstance(WeChatRespBean.class);

Marshaller m = jc.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

m.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {@Override

public void escape(char[] arg0, int arg1, int arg2, boolean arg3,Writer arg4) throws IOException {

arg4.write(arg0, arg1, arg2);}

});m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

WeChatRespBean respBean = createRespBean(reqBean, content);m.marshal(respBean, out);

out.flush();

}catch (JAXBException e) {

e.printStackTrace();} finally {

if (scanner != null) {scanner.close();

scanner = null;}

if (out != null) {out.close();

out = null;}

}}

/**

* @param reqBean* @param content

* @return*/

private WeChatRespBean createRespBean(WeChatReqBean reqBean, String content) {WeChatRespBean respBean = new WeChatRespBean();

respBean.setFromUserName(reqBean.getToUserName());respBean.setToUserName(reqBean.getFromUserName());

respBean.setMsgType("text");respBean.setCreateTime(new Date().getTime());

respBean.setContent(content);return respBean;

}

public String Encrypt(String strSrc) {Message葡京娱乐场的官网Digest md = null;

String strDes = null;

byte[] bt = strSrc.getBytes();try {

md = MessageDigest.getInstance("SHA-1");md.update(bt);

strDes = bytes2Hex(md.digest()); //to HexString} catch (NoSuchAlgorithmException e) {

System.out.println("Invalid algorithm.");return null;

}return strDes;

}

public String bytes2Hex(byte[] bts) {String des = "";

String tmp = null;for (int i = 0; i

tmp = (Integer.toHexStri葡京娱乐场的官网ng(bts[i] & 0xFF));if (tmp.length() == 1) {

des += "0";}

des += tmp;}

return des;}

/**

* @param reqBean* @throws JAXBException

* @throws MalformedURLException*/

private String getContent(WeChatReqBean reqBean) throws JAXBException, MalformedURLException {StringBuffer content = new StringBuffer("");

switch (Constants.getType(reqBean.getMsgType())){

case text:if (reqBean.getContent().contains("生长档案")) {

// 这是新用户关注时默认发的一条信息。可以做一个迎接处置惩罚。content.append("这是生长档案的先容!"+"\r\n");

content.append(""+"\r\n");content.append(""+"\r\n");

content.append("回覆h获取更多内容!"+"\r\n");}else if(reqBean.getContent().contains("h")){

content.append("这是赞助信息!"+"\r\n");content.append("假如你还有疑问或建议:请以#开首发送翰墨内容,我们的客服收到留言后会尽快跟你联系。"+"\r\n");

}else if(reqBean.getContent().contains("cp")){content.append("产品先容:"+"\r\n");

content.append("001.家园共育平台"+"\r\n");content.append("002.运营阐发系统"+"\r\n");

content.append("003.互动私塾"+"\r\n");content.append(""+"\r\n");

content.append(""+"\r\n");content.append("回覆数字获取更多内容!"+"\r\n");

}

else {content.append("你的信息已收到,我们会第一光阴处置惩罚.");

}break;

case event:if("subscribe".equals(reqBean.getEvent())){

// 这是新用户关注时默认发的一条信息。可以做一个迎接处置惩罚。content.append("异常谢谢你的添加,我们准时为你供给最新的儿童内容办事哟!"+"\r\n");

content.append("1.想懂得我们的产品请直接将产品名称回覆给我(如\"生长档案\")"+"\r\n");content.append("2.想知道已经上线了哪些产品请回覆\"cp\"或直接点击链接www.fengfly.com进行查看"+"\r\n");

content.append("3.如需赞助或懂得请回覆\"h\"。"+"\r\n");}

break;case image:

break;

default:content.append("这是一个测试!!!");

break;}

return content.toString();}

}

您可能还会对下面的文章感兴趣: