回复图文消息只有第一个图文的说明会被显示出来,但是开发文档又没有说.
与服务器传文件,搞一个FTP/QQ/复制粘贴.
package net.wxinterface;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
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.DocumentException;
import wx.event.Subscribe_Unsubscribe;
import wx.in.In_Image;
import wx.in.In_Text;
import wx.in.In_Util;
import wx.in.In_Video;
import wx.in.In_Voice;
import wx.out.Out_Image;
import wx.out.Out_News;
import wx.out.Out_Text;
import wx.out.Out_Video;
import wx.out.Out_Voice;
import wx.util.Util;
import com.imooc.po.TextMessage;
import com.imooc.util.MessageUtil;
public class WX_Interface extends HttpServlet {
/**
* Constructor of the object.
*/
public WX_Interface() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//�ż���ǩ��,signature����˿�������д��tocken����������е�timestamp����nonce����
String signature = request.getParameter("signature");
//ʱ���
String timestamp = request.getParameter("timestamp");
//�����
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
String tocken = "test";
try{
if(null != signature){
String[] ArrTmp = {tocken,timestamp,nonce};
Arrays.sort(ArrTmp);
StringBuffer sb = new StringBuffer();
for(int i=0;i<ArrTmp.length;i++){
sb.append(ArrTmp[i]);
}
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(new String(sb).getBytes());
StringBuffer buf = new StringBuffer();
for(int i=0;i<bytes.length;i++){
if(((int)bytes[i] & 0xff)<0x10){
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff,16));
}
if(signature.equals(buf.toString())){
response.getOutputStream().println(echostr);
}
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("test0");
System.out.println("doGet");
System.out.println("signature "+signature);
System.out.println("timstamp "+timestamp);
System.out.println("nonce "+nonce);
System.out.println("echostr "+echostr);
System.out.println("doGet");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
Map<String,String> 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("text".equals(msgType)){
if(MessageUtil.MESSAGE_TEXT.equals(msgType)){
if("1".equals(content)){
message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.firstMenu());
}else if("3".equals(content)){
//message = MessageUtil.initText(toUserName,fromUserName,content);
//�Ϳ��������ﰴ3����ͼƬ��Ϣ,���ͼƬ��Ϣ�Ĵ����Լ��ظ��Ѿ�д����
message = MessageUtil.initImageMessage(toUserName,fromUserName);
}else if("4".equals(content)){//��servlet����һ���ж�,������4��ʱ��ظ�һ�����ָ���
message = MessageUtil.initMusicMessage(toUserName,fromUserName);
}else if("2".equals(content)){
//message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.secondMenu());
//�����ͼ����Ϣ�Ĵ����Լ��ظ��Ѿ�д����
message= MessageUtil.initNewsMessage(toUserName,fromUserName);
}else if("?".equals(content)||" ? ".equals(content)){
message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.menuText());
}
TextMessage text = new TextMessage();
text.setFromUserName(toUserName);//˭������,��ͷ���˭
text.setToUserName(fromUserName);
text.setMsgType("text");
text.setCreateTime(new Date().getTime());
text.setContent("���͵���Ϣ��:"+content);
message = MessageUtil.textMessageToXml(text);
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, content);//��ע�Ź��ں�֮���ź�̨����
}//�������Servlet�������һ�������ж�
else if(MessageUtil.MESSAGE_CLICK.equals(eventType)){//�����������Click�˵���һ���¼�����
//Click���͵���Ϣ����,���Event��Click���͵Ļ�,��ô����ظ�һ�����˵�
message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
}else if(MessageUtil.MESSAGE_VIEW.equals(eventType)){//�����View���Ͳ˵�,�ͻظ�һ�������õ�Url
String url = map.get("EventKey");//�����View���Ͳ˵��Ļ�,���EventKey�������Url��ֵ
if("11".equals(url)){//���Keyֵ��11
message = MessageUtil.initText(toUserName, fromUserName, url);
}
}else if(MessageUtil.MESSAGE_SCANCODE.equals(eventType)){//�ظ�һ�����ɨ���¼�
String key = map.get("EventKey");//�����Click���Ͳ˵��Ļ�,EventKey���������õ�Keyֵ
message = MessageUtil.initText(toUserName, fromUserName, key);
}
}
out .print(message);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
System.out.println("doPost");*/
//response.setContentType("text/html");
//PrintWriter out = response.getWriter();
/* StringBuffer strb = new StringBuffer();
ServletInputStream in = request.getInputStream();
BufferedReader breader= new BufferedReader( new InputStreamReader(in,"UTF-8"));
String str = null;
while(null!=(str=breader.readLine())){
strb.append(str);
}
//out.println(str);
System.out.println(strb);*/
//out.flush();
//out.close();
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();
StringBuffer strb = new StringBuffer();
//strb要存储一下request获取过来的东西
ServletInputStream in = request.getInputStream();
BufferedReader breader = new BufferedReader( new InputStreamReader(in,"UTF-8"));//接收是对的,已经进行转码了.
String str = null;
//+-*\/\是运算符,赋值运算符=也是运算符
/*
while(null!=(str = breader.readLine())){
strb.append(str);
}
out.println(strb);
System.out.println(strb);
out.flush();
out.close();*/
//response.setContentType("text/html;charset=utf-8");
//response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
//String requestStr = In_Util.getStringFromRequest(request);
String requestStr = Util.getStringFromRequest(request);
System.out.println(requestStr);
System.out.println("北风网");
/* In_Text in_Text = new In_Text(requestStr);
System.out.println(in_Text);
in_Text.setContent(in_Text.getContent()+ " 吃屎啦");*/
/* In_Image in_Image = new In_Image(requestStr);
System.out.println(in_Image);*/
//Out_Text out_Text = new Out_Text(in_Text);
//Out_Text out_Text = new Out_Text(in_Text,in_Text.getContent()+ " 吃屎啦");
/* Out_Text out_Text = new Out_Text(in_Text);
out_Text.setContent(in_Text.getContent());*/
//System.out.println(out_Text.getStr("这是北风网。。。。。。。"));
//response.getOutputStream().print(out_Text.getStr(in_Text.getContent()+ " 吃屎啦"));
//response.getWriter().print(out_Text.getStr(in_Text.getContent()+ " 吃屎啦"));
//response.getWriter().print(out_Text.getStr());
//new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"UTF-8")).write(out_Text.getStr(in_Text.getContent()+ " chishila"));
//response.getOutputStream().print(out_Text.getStr("这是北风网。。。。。。。"));
//System.out.println(out_Text.getStr(in_Text.getContent()+ " 吃屎啦"));
//System.out.println(out_Text.getStr());
/*In_Image in_Image = new In_Image(requestStr);
System.out.println(in_Image.getMediaId());
Out_Image out_Image = new Out_Image(in_Image);
out_Image.setMediaId(in_Image.getMediaId());
response.getWriter().print(out_Image.getStr());*/
/* String shanghai = "szNaZ9h5SbG4kMDlCexVMvNLuezqTB20QE9XOgpbK5plhmCOnaO6JG6-ISXsIdvn";
String xxx= "T9Bi3d-qxYbZnE6f1CsYJ2EfNrWDdr9x3Kpas_WUo04tc9b4zwTq9KZs1vQ6taQB";
In_Text in_Text = new In_Text(requestStr);
Out_Image out_Image = new Out_Image(in_Text);
if("1".equals(in_Text.getContent())){
out_Image.setMediaId(shanghai);
}else{
out_Image.setMediaId(xxx);
}
response.getWriter().print(out_Image.getStr());*/
/* In_Voice in_Voice = new In_Voice(requestStr);
Out_Voice out_Voice = new Out_Voice(in_Voice);
out_Voice.setMediaId(in_Voice.getMediaId());
response.getWriter().print(out_Voice.getStr());*/
/* In_Video in_Video = new In_Video(requestStr);
Out_Video out_Video = new Out_Video(in_Video);
out_Video.setMediaId(in_Video.getMediaId());
out_Video.setTitle("title");
out_Video.setDescription("description");
System.out.println(out_Video.getStr());
//response.getWriter().print(out_Video.getStr());
response.getOutputStream().print(out_Video.getStr());*/
Subscribe_Unsubscribe subscribe_Unsubscribe = new Subscribe_Unsubscribe(requestStr);
System.out.println(subscribe_Unsubscribe);
Out_News out_News = new Out_News(subscribe_Unsubscribe);
List<Out_News> out_NewsList = new ArrayList<Out_News>();
Out_News out_News_0 = new Out_News();
out_News_0.setTitle("北风网微信系统");
out_News_0.setDescription("北风网");
out_News_0.setPicUrl("http://www.ibeifeng.com/themes/default/images/logo.png");
out_News_0.setUrl("http://www.ibeifeng.com/");
out_NewsList.add(out_News_0);
Out_News out_News_1 = new Out_News();
out_News_1.setTitle("java北风网微信系统");
out_News_1.setDescription("北风网xxxxxxxxxxxxx");
out_News_1.setPicUrl("http://www.ibeifeng.com/data/indeximg/a2.jpg");
out_News_1.setUrl("http://www.ibeifeng.com/tc_java2.html");
out_NewsList.add(out_News_1);
Out_News out_News_2 = new Out_News();
out_News_2.setTitle(".net北风网微信系统");
out_News_2.setDescription("北风网AAAAAAAAAAAA");
out_News_2.setPicUrl("http://www.ibeifeng.com/data/indeximg/a3.jpg");
out_News_2.setUrl("http://www.ibeifeng.com/netcourse.html");
out_NewsList.add(out_News_2);
System.out.println(out_News.getStr(out_NewsList));
response.getWriter().print(out_News.getStr(out_NewsList));
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
package wx.event;
import java.util.Date;
import wx.in.In;
import wx.util.Util;
public class Subscribe_Unsubscribe extends In{
/* private String ToUserName =null;// 开发者微信号
private String FromUserName = null;// 发送方帐号(一个OpenID)
private String CreateTime = null;// 消息创建时间 (整型)
private String MsgType = null;// 消息类型,event
*/ private String Event = null;// 事件类型,subscribe(订阅)、unsubscribe(取消订阅)
public Subscribe_Unsubscribe() {
super();
}
public Subscribe_Unsubscribe(String requestStr) {
Date date = new Date();
this.setToUserName(Util.getStrByXML(requestStr,"ToUserName"));
this.setFromUserName(Util.getStrByXML(requestStr,"FromUserName"));
//this.setCreateTime(date.getTime()+"");
//this.setMsgType("MsgType");
this.setCreateTime(Util.getStrByXML(requestStr,"CreateTime"));
this.setMsgType(Util.getStrByXML(requestStr,"MsgType"));
this.setEvent(Util.getStrByXML(requestStr,"Event"));
}
/*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 getEvent() {
return Event;
}
public void setEvent(String event) {
Event = event;
}
@Override
public String toString() {
return "Subscribe_Unsubscribe [Event=" + Event + "]";
}
}
package wx.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
public class Util {
/**
* 通过request得到请求流中的微信报文str
* @param request
* @return
*/
public static String getStringFromRequest(HttpServletRequest request){
StringBuffer strb = new StringBuffer();
//strb要存储一下request获取过来的东西
BufferedReader breader = null;
//ServletInputStream in;
try {
//in = request.getInputStream();
//breader = new BufferedReader( new InputStreamReader(in,"UTF-8"));//接收是对的,已经进行转码了.
breader = new BufferedReader( new InputStreamReader(request.getInputStream(),"UTF-8"));//接收是对的,已经进行转码了.
String str = null;
//+-*/是运算符,赋值运算符=也是运算符
while(null!=(str = breader.readLine())){
strb.append(str);
}
breader.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strb.toString();
}
/**
* 得到微信的相关文字
* @param str
* @return
*/
public static String getStrByXML(String str,String tagName){
//解析XML,使用dom4j或者是SAX,JDOM这类框架.
//使用最基本的字符串处理它
//可以使用解析XML的第三方框架或者是工具包,dom4j之类的.
String begin = "<"+tagName+"><![CDATA[";
String end = "]]></"+tagName+">";
boolean flag = false;
if("CreateTime".equals(tagName)){
flag = true;
}else if("MsgId".equals(tagName)){
flag = true;
}else if("Location_X".equals(tagName)){
flag = true;
}else if("Location_Y".equals(tagName)){
flag = true;
}else if("Scale".equals(tagName)){
flag = true;
}
if(flag ){
begin = "<"+tagName+">";
end = "</"+tagName+">";
}
System.out.println(begin);
System.out.println(end);
int beginIndex = str.indexOf(begin) + begin.length();//
int endIndex = str.indexOf(end);
return str.substring(beginIndex,endIndex);
}
}
import java.util.ArrayList;
import java.util.List;
import wx.in.In;
import wx.in.In_Image;
import wx.in.In_Text;
import wx.in.In_Util;
import wx.out.Out_Music;
import wx.out.Out_News;
import wx.out.Out_Text;
@SuppressWarnings("unused")
public class Test {
/**
*
* @param args
*/
public static void main(String[] args) {
//text
String str = "<xml><ToUserName><![CDATA[gh_44e4d096da50]]></ToUserName><FromUserName><![CDATA[o2d2nwwoWhw1P2VDWiQHj1brqSc4]]></FromUserName><CreateTime>1495014482</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[北风网]]></Content><MsgId>6421038307652509766</MsgId></xml>";
//图
/* System.out.println(In_Util.getStrByXML(str,"ToUserName"));
System.out.println(In_Util.getStrByXML(str,"FromUserName"));*/
In_Text in_Text = new In_Text(str);
/*
System.out.println(in_Text);
In_Image in_Image = new In_Image(str);
*/
/* In in = new In(str);
System.out.println(in);*/
/* Out_Text out_Text = new Out_Text(in_Text);
System.out.println(out_Text.getStr("这是北风网。。。。。。。"));*/
/* Out_Music out_Music = new Out_Music(in_Text);
out_Music.setTitle("title");
out_Music.setDescription("description");
out_Music.setMusicURL("musicURL");
out_Music.setHQMusicUrl("hQMusicUrl");
out_Music.setThumbMediaId("thumbMediaId");
System.out.println(out_Music.getStr());*/
Out_News out_News = new Out_News(in_Text);
List<Out_News> out_NewsList = new ArrayList<Out_News>();
for(int x=0;x<4;x++){
Out_News news = new Out_News();
news.setTitle("title"+x);
news.setDescription("description"+x);
news.setPicUrl("picUrl"+x);
news.setUrl("url"+x);
out_NewsList.add(news);
}
System.out.println(out_News.getStr(out_NewsList));
}
}