1 package xidian.wq.wechatlib.entity.receive; 2 3 4 /** 5 * 6 * 項目名稱:wechatlib 7 * 類名稱:ReceiveBaseMessage 8 * 類描述:接收消息基類(普通用戶發送信息給公衆賬號) 9 * 建立人:WQ 10 * 建立時間:2013-10-3 下午4:12:57 11 * @version 12 */ 13 public class ReceiveBaseMessage { 14 // 開發者微信號 15 private String ToUserName; 16 // 發送方賬號(OpenID) 17 private String FromUserName; 18 // 消息建立時間 (整型) 19 private long CreateTime; 20 // 消息類型(text/image/location/link) 21 private String MsgType; 22 // 消息id,64位整型 23 private long MsgId; 24 25 public String getToUserName() { 26 return ToUserName; 27 } 28 29 public void setToUserName(String toUserName) { 30 ToUserName = toUserName; 31 } 32 33 public String getFromUserName() { 34 return FromUserName; 35 } 36 37 public void setFromUserName(String fromUserName) { 38 FromUserName = fromUserName; 39 } 40 41 public long getCreateTime() { 42 return CreateTime; 43 } 44 45 public void setCreateTime(long createTime) { 46 CreateTime = createTime; 47 } 48 49 public String getMsgType() { 50 return MsgType; 51 } 52 53 public void setMsgType(String msgType) { 54 MsgType = msgType; 55 } 56 57 public long getMsgId() { 58 return MsgId; 59 } 60 61 public void setMsgId(long msgId) { 62 MsgId = msgId; 63 } 64 }
請求消息中的文本消息java
1 package xidian.wq.wechatlib.entity.receive; 2 3 /** 4 * 5 * 項目名稱:wechatlib 6 * 類名稱:TextMessage 7 * 類描述:文本消息 8 * 建立人:WQ 9 * 建立時間:2013-10-3 下午4:13:06 10 * @version 11 */ 12 public class TextMessage extends ReceiveBaseMessage{ 13 // 消息內容 14 private String Content; 15 16 public String getContent() { 17 return Content; 18 } 19 20 public void setContent(String content) { 21 Content = content; 22 } 23 }
請求消息中的圖片消息git
1 package xidian.wq.wechatlib.entity.receive; 2 3 4 /** 5 * 6 * 項目名稱:wechatlib 7 * 類名稱:ImageMessage 8 * 類描述:圖片消息 9 * 建立人:WQ 10 * 建立時間:2013-10-3 下午4:12:19 11 * @version 12 */ 13 public class ImageMessage extends ReceiveBaseMessage{ 14 // 圖片連接 15 private String PicUrl; 16 17 public String getPicUrl() { 18 return PicUrl; 19 } 20 21 public void setPicUrl(String picUrl) { 22 PicUrl = picUrl; 23 } 24 }
請求消息中的地理位置消息web
1 package xidian.wq.wechatlib.entity.receive; 2 3 4 /** 5 * 6 * 項目名稱:wechatlib 7 * 類名稱:LocationMessage 8 * 類描述:地理位置消息 9 * 建立人:WQ 10 * 建立時間:2013-10-3 下午4:12:45 11 * @version 12 */ 13 public class LocationMessage extends ReceiveBaseMessage{ 14 // 地理位置維度 15 private String Location_X; 16 // 地理位置經度 17 private String Location_Y; 18 // 地圖縮放大小 19 private String Scale; 20 // 地理位置信息 21 private String Label; 22 23 public String getLocation_X() { 24 return Location_X; 25 } 26 27 public void setLocation_X(String location_X) { 28 Location_X = location_X; 29 } 30 31 public String getLocation_Y() { 32 return Location_Y; 33 } 34 35 public void setLocation_Y(String location_Y) { 36 Location_Y = location_Y; 37 } 38 39 public String getScale() { 40 return Scale; 41 } 42 43 public void setScale(String scale) { 44 Scale = scale; 45 } 46 47 public String getLabel() { 48 return Label; 49 } 50 51 public void setLabel(String label) { 52 Label = label; 53 } 54 }
請求消息中的連接消息數組
1 package xidian.wq.wechatlib.entity.receive; 2 3 /** 4 * 5 * 項目名稱:wechatlib 6 * 類名稱:LinkMessage 7 * 類描述:連接消息 8 * 建立人:WQ 9 * 建立時間:2013-10-3 下午4:12:33 10 * @version 11 */ 12 public class LinkMessage { 13 // 消息標題 14 private String Title; 15 // 消息描述 16 private String Description; 17 // 消息連接 18 private String Url; 19 20 public String getTitle() { 21 return Title; 22 } 23 24 public void setTitle(String title) { 25 Title = title; 26 } 27 28 public String getDescription() { 29 return Description; 30 } 31 32 public void setDescription(String description) { 33 Description = description; 34 } 35 36 public String getUrl() { 37 return Url; 38 } 39 40 public void setUrl(String url) { 41 Url = url; 42 } 43 }
請求消息中的語音消息tomcat
1 package xidian.wq.wechatlib.entity.receive; 2 3 /** 4 * 5 * 項目名稱:wechatlib 6 * 類名稱:VoiceMessage 7 * 類描述:音頻消息 8 * 建立人:WQ 9 * 建立時間:2013-10-3 下午4:13:18 10 * @version 11 */ 12 public class VoiceMessage { 13 // 媒體ID 14 private String MediaId; 15 // 語音格式 16 private String Format; 17 18 public String getMediaId() { 19 return MediaId; 20 } 21 22 public void setMediaId(String mediaId) { 23 MediaId = mediaId; 24 } 25 26 public String getFormat() { 27 return Format; 28 } 29 30 public void setFormat(String format) { 31 Format = format; 32 } 33 }
b.回覆消息封裝(xidian.wq.wechatlib.entity.send)服務器
回覆消息的基類微信
1 package xidian.wq.wechatlib.entity.send; 2 3 /** 4 * 5 * 項目名稱:wechatlib 6 * 類名稱:SendBaseMessage 7 * 類描述:回覆消息基類(公衆賬號回覆消息給普通用戶) 8 * 建立人:WQ 9 * 建立時間:2013-10-3 下午4:11:46 10 * @version 11 */ 12 public class SendBaseMessage { 13 // 接收方賬號(OpenID) 14 private String ToUserName; 15 // 開發者微信號 16 private String FromUserName; 17 // 消息建立時間 (整型) 18 private long CreateTime; 19 // 消息類型(text/music/news) 20 private String MsgType; 21 22 public String getToUserName() { 23 return ToUserName; 24 } 25 26 public void setToUserName(String toUserName) { 27 ToUserName = toUserName; 28 } 29 30 public String getFromUserName() { 31 return FromUserName; 32 } 33 34 public void setFromUserName(String fromUserName) { 35 FromUserName = fromUserName; 36 } 37 38 public long getCreateTime() { 39 return CreateTime; 40 } 41 42 public void setCreateTime(long createTime) { 43 CreateTime = createTime; 44 } 45 46 public String getMsgType() { 47 return MsgType; 48 } 49 50 public void setMsgType(String msgType) { 51 MsgType = msgType; 52 } 53 }
回覆消息中的文本消息app
1 package xidian.wq.wechatlib.entity.send; 2 3 /** 4 * 5 * 項目名稱:wechatlib 6 * 類名稱:TextMessage 7 * 類描述:文本消息 8 * 建立人:WQ 9 * 建立時間:2013-10-3 下午4:12:00 10 * @version 11 */ 12 public class TextMessage extends SendBaseMessage{ 13 // 消息內容 14 private String Content; 15 16 public String getContent() { 17 return Content; 18 } 19 20 public void setContent(String content) { 21 Content = content; 22 } 23 }
回覆消息中的音樂消息微信公衆平臺
1 package xidian.wq.wechatlib.entity.send; 2 3 4 /** 5 * 6 * 項目名稱:wechatlib 7 * 類名稱:MusicMessage 8 * 類描述:音樂消息 9 * 建立人:WQ 10 * 建立時間:2013-10-3 下午4:11:19 11 * @version 12 */ 13 public class MusicMessage extends SendBaseMessage{ 14 // 音樂 15 private Music Music; 16 17 public Music getMusic() { 18 return Music; 19 } 20 21 public void setMusic(Music music) { 22 Music = music; 23 } 24 }
音樂消息中的music類dom
1 package xidian.wq.wechatlib.entity.send; 2 3 4 /** 5 * 6 * 項目名稱:wechatlib 7 * 類名稱:Music 8 * 類描述:音樂model 9 * 建立人:WQ 10 * 建立時間:2013-10-3 下午4:11:03 11 * @version 12 */ 13 public class Music { 14 // 音樂名稱 15 private String Title; 16 // 音樂描述 17 private String Description; 18 // 音樂連接 19 private String MusicUrl; 20 // 高質量音樂連接,WIFI環境優先使用該連接播放音樂 21 private String HQMusicUrl; 22 23 public String getTitle() { 24 return Title; 25 } 26 27 public void setTitle(String title) { 28 Title = title; 29 } 30 31 public String getDescription() { 32 return Description; 33 } 34 35 public void setDescription(String description) { 36 Description = description; 37 } 38 39 public String getMusicUrl() { 40 return MusicUrl; 41 } 42 43 public void setMusicUrl(String musicUrl) { 44 MusicUrl = musicUrl; 45 } 46 47 public String getHQMusicUrl() { 48 return HQMusicUrl; 49 } 50 51 public void setHQMusicUrl(String musicUrl) { 52 HQMusicUrl = musicUrl; 53 } 54 }
回覆消息中的圖文消息
1 package xidian.wq.wechatlib.entity.send; 2 3 import java.util.List; 4 5 6 /** 7 * 8 * 項目名稱:wechatlib 9 * 類名稱:NewsMessage 10 * 類描述:圖文消息 11 * 建立人:WQ 12 * 建立時間:2013-10-3 下午4:11:32 13 * @version 14 */ 15 public class NewsMessage extends SendBaseMessage{ 16 // 圖文消息個數,限制爲10條之內 17 private int ArticleCount; 18 // 多條圖文消息信息,默認第一個item爲大圖 19 private List<Article> Articles; 20 21 public int getArticleCount() { 22 return ArticleCount; 23 } 24 25 public void setArticleCount(int articleCount) { 26 ArticleCount = articleCount; 27 } 28 29 public List<Article> getArticles() { 30 return Articles; 31 } 32 33 public void setArticles(List<Article> articles) { 34 Articles = articles; 35 } 36 }
圖文消息中的article類
1 package xidian.wq.wechatlib.entity.send; 2 3 4 /** 5 * 6 * 項目名稱:wechatlib 7 * 類名稱:Article 8 * 類描述:圖文model 9 * 建立人:WQ 10 * 建立時間:2013-10-3 下午4:10:51 11 * @version 12 */ 13 public class Article { 14 // 圖文消息名稱 15 private String Title; 16 // 圖文消息描述 17 private String Description; 18 // 圖片連接,支持JPG、PNG格式,較好的效果爲大圖600*300,小圖80*80,限制圖片連接的域名須要與開發者填寫的基本資料中的Url一致 19 private String PicUrl; 20 // 點擊圖文消息跳轉連接 21 private String Url; 22 23 public String getTitle() { 24 return Title; 25 } 26 27 public void setTitle(String title) { 28 Title = title; 29 } 30 31 public String getDescription() { 32 return null == Description ? "" : Description; 33 } 34 35 public void setDescription(String description) { 36 Description = description; 37 } 38 39 public String getPicUrl() { 40 return null == PicUrl ? "" : PicUrl; 41 } 42 43 public void setPicUrl(String picUrl) { 44 PicUrl = picUrl; 45 } 46 47 public String getUrl() { 48 return null == Url ? "" : Url; 49 } 50 51 public void setUrl(String url) { 52 Url = url; 53 } 54 }
c.基礎設置(xidian.wq.wechatlib.utils)
消息處理工具類
1 package xidian.wq.wechatlib.utils; 2 3 import java.io.InputStream; 4 import java.io.Writer; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import javax.servlet.http.HttpServletRequest; 10 11 import org.dom4j.Document; 12 import org.dom4j.Element; 13 import org.dom4j.io.SAXReader; 14 15 import xidian.wq.wechatlib.entity.send.Article; 16 import xidian.wq.wechatlib.entity.send.MusicMessage; 17 import xidian.wq.wechatlib.entity.send.NewsMessage; 18 import xidian.wq.wechatlib.entity.send.TextMessage; 19 20 import com.thoughtworks.xstream.XStream; 21 import com.thoughtworks.xstream.core.util.QuickWriter; 22 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 23 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; 24 import com.thoughtworks.xstream.io.xml.XppDriver; 25 26 /** 27 * 28 * 項目名稱:wechatlib 29 * 類名稱:MessageUtil 30 * 類描述:消息處理工具類 31 * 建立人:WQ 32 * 建立時間:2013-10-3 下午4:08:40 33 * @version 34 */ 35 public class MessageUtil { 36 /** 37 * 返回消息類型:文本 38 */ 39 public static final String SEND_TEXT = "text"; 40 41 /** 42 * 返回消息類型:音樂 43 */ 44 public static final String SEND_MUSIC = "music"; 45 46 /** 47 * 返回消息類型:圖文 48 */ 49 public static final String SEND_NEWS = "news"; 50 51 /** 52 * 請求消息類型:文本 53 */ 54 public static final String RECRIVE_TEXT = "text"; 55 56 /** 57 * 請求消息類型:圖片 58 */ 59 public static final String RECRIVE_IMAGE = "image"; 60 61 /** 62 * 請求消息類型:連接 63 */ 64 public static final String RECRIVE_LINK = "link"; 65 66 /** 67 * 請求消息類型:地理位置 68 */ 69 public static final String RECRIVE_LOCATION = "location"; 70 71 /** 72 * 請求消息類型:音頻 73 */ 74 public static final String RECRIVE_VOICE = "voice"; 75 76 /** 77 * 請求消息類型:推送 78 */ 79 public static final String RECRIVE_EVENT = "event"; 80 81 /** 82 * 事件類型:subscribe(訂閱) 83 */ 84 public static final String RECRIVE_SUBSCRIBE = "subscribe"; 85 86 /** 87 * 事件類型:unsubscribe(取消訂閱) 88 */ 89 public static final String RECRIVE_UNSUBSCRIBE = "unsubscribe"; 90 91 /** 92 * 事件類型:CLICK(自定義菜單點擊事件) 93 */ 94 public static final String RECRIVE_CLICK = "CLICK"; 95 96 /** 97 * 解析微信發來的請求(XML) 98 * 99 * @param request 100 * @return 101 * @throws Exception 102 */ 103 @SuppressWarnings("unchecked") 104 public static Map<String, String> parseXml(HttpServletRequest request) throws Exception { 105 // 將解析結果存儲在HashMap中 106 Map<String, String> map = new HashMap<String, String>(); 107 // 從request中取得輸入流 108 InputStream inputStream = request.getInputStream(); 109 // 讀取輸入流 110 SAXReader reader = new SAXReader(); 111 Document document = reader.read(inputStream); 112 // 獲得xml根元素 113 Element root = document.getRootElement(); 114 // 獲得根元素的全部子節點 115 List<Element> elementList = root.elements(); 116 // 遍歷全部子節點 117 for (Element e : elementList) 118 map.put(e.getName(), e.getText()); 119 // 釋放資源 120 inputStream.close(); 121 inputStream = null; 122 return map; 123 } 124 125 /** 126 * 文本消息對象轉換成xml 127 * 128 * @param textMessage 文本消息對象 129 * @return xml 130 */ 131 public static String textMessageToXml(TextMessage textMessage) { 132 xstream.alias("xml", textMessage.getClass()); 133 return xstream.toXML(textMessage); 134 } 135 136 /** 137 * 音樂消息對象轉換成xml 138 * 139 * @param musicMessage 音樂消息對象 140 * @return xml 141 */ 142 public static String musicMessageToXml(MusicMessage musicMessage) { 143 xstream.alias("xml", musicMessage.getClass()); 144 return xstream.toXML(musicMessage); 145 } 146 147 /** 148 * 圖文消息對象轉換成xml 149 * 150 * @param newsMessage 圖文消息對象 151 * @return xml 152 */ 153 public static String newsMessageToXml(NewsMessage newsMessage) { 154 xstream.alias("xml", newsMessage.getClass()); 155 xstream.alias("item", new Article().getClass()); 156 return xstream.toXML(newsMessage); 157 } 158 159 160 /** 161 * 擴展xstream,使其支持CDATA塊 162 * 163 * @date 2013-05-19 164 */ 165 private static XStream xstream = new XStream(new XppDriver() { 166 public HierarchicalStreamWriter createWriter(Writer out) { 167 return new PrettyPrintWriter(out) { 168 // 對全部xml節點的轉換都增長CDATA標記 169 boolean cdata = true; 170 @SuppressWarnings("unchecked") 171 public void startNode(String name, Class clazz) { 172 super.startNode(name, clazz); 173 } 174 protected void writeText(QuickWriter writer, String text) { 175 if (cdata) { 176 writer.write("<![CDATA["); 177 writer.write(text); 178 writer.write("]]>"); 179 } else { 180 writer.write(text); 181 } 182 } 183 }; 184 } 185 }); 186 }
url配置請求校驗工具類
1 package xidian.wq.wechatlib.utils; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 import java.util.Arrays; 6 7 import java.security.MessageDigest; 8 import java.security.NoSuchAlgorithmException; 9 import java.util.Arrays; 10 11 12 /** 13 * 14 * 項目名稱:wechatlib 15 * 類名稱:SignUtil 16 * 類描述:url配置請求校驗工具類 17 * 建立人:WQ 18 * 建立時間:2013-10-3 下午4:09:14 19 * @version 20 */ 21 public class SignUtil { 22 // 與接口配置信息中的Token要一致 23 private static String token = "fromsedion"; 24 /** 25 * 驗證簽名 26 * 27 * @param signature 微信加密簽名 28 * @param timestamp 時間戳 29 * @param nonce 隨機數 30 * @return 31 */ 32 public static boolean checkSignature(String signature, String timestamp, String nonce) { 33 String[] arr = new String[] { token, timestamp, nonce }; 34 // 將token、timestamp、nonce三個參數進行字典序排序 35 Arrays.sort(arr); 36 StringBuilder content = new StringBuilder(); 37 for (int i = 0; i < arr.length; i++) { 38 content.append(arr[i]); 39 } 40 MessageDigest md = null; 41 String tmpStr = null; 42 43 try { 44 md = MessageDigest.getInstance("SHA-1"); 45 // 將三個參數字符串拼接成一個字符串進行sha1加密 46 byte[] digest = md.digest(content.toString().getBytes()); 47 tmpStr = byteToStr(digest); 48 } catch (NoSuchAlgorithmException e) { 49 e.printStackTrace(); 50 } 51 52 content = null; 53 // 將sha1加密後的字符串可與signature對比,標識該請求來源於微信 54 return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false; 55 } 56 57 /** 58 * 將字節數組轉換爲十六進制字符串 59 * 60 * @param byteArray 61 * @return 62 */ 63 private static String byteToStr(byte[] byteArray) { 64 String strDigest = ""; 65 for (int i = 0; i < byteArray.length; i++) { 66 strDigest += byteToHexStr(byteArray[i]); 67 } 68 return strDigest; 69 } 70 71 /** 72 * 將字節轉換爲十六進制字符串 73 * 74 * @param mByte 75 * @return 76 */ 77 private static String byteToHexStr(byte mByte) { 78 char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 79 char[] tempArr = new char[2]; 80 tempArr[0] = Digit[(mByte >>> 4) & 0X0F]; 81 tempArr[1] = Digit[mByte & 0X0F]; 82 String s = new String(tempArr); 83 return s; 84 } 85 }
d.service回覆信息處理(xidian.wq.wechatlib.ways.send)
1 package xidian.wq.wechatlib.ways.send; 2 import java.util.Date; 3 import java.util.List; 4 5 import xidian.wq.wechatlib.entity.send.Article; 6 import xidian.wq.wechatlib.entity.send.NewsMessage; 7 import xidian.wq.wechatlib.entity.send.TextMessage; 8 import xidian.wq.wechatlib.utils.MessageUtil; 9 10 /** 11 * 12 * 項目名稱:wechatlib 13 * 類名稱:SendService 14 * 類描述: 回覆信息類型的封裝 15 * 建立人:WQ 16 * 建立時間:2013-10-3 下午4:07:59 17 * @version 18 */ 19 public class SendService { 20 /** 21 * 回覆文本消息 22 * @param fromusername 粉絲openid 23 * @param tousername 微信公衆號 24 * @param respContent 回覆信息 25 * @return 26 */ 27 public static String sendTextmessage(String fromusername,String tousername,String respContent){ 28 //初始化回覆信息 29 String respmessage; 30 //回覆文本消息 31 TextMessage textMessage = new TextMessage(); 32 //發送方賬號(一個OpenID) 33 textMessage.setToUserName(fromusername); 34 //開發者微信號 35 textMessage.setFromUserName(tousername); 36 //消息建立時間 (整型) 37 textMessage.setCreateTime(new Date().getTime()); 38 //消息類型text 39 textMessage.setMsgType(MessageUtil.SEND_TEXT); 40 //回覆的消息內容,長度不超過2048字節 41 textMessage.setContent(respContent); 42 //轉爲xml格式 43 respmessage = MessageUtil.textMessageToXml(textMessage); 44 //返回回覆信息 45 return respmessage; 46 } 47 48 /** 49 * 圖文消息設置 50 * @param fromusername 粉絲openid 51 * @param tousername 開發者微信公衆帳號 52 * @param newslist 圖文消息list 53 * @return 54 */ 55 public static String sendNewsmessage(String fromusername,String tousername,List<Article> newslist){ 56 //初始化回覆信息 57 String respmessage; 58 //建立圖文消息 59 NewsMessage newsMessage=new NewsMessage(); 60 //發送方賬號(一個OpenID) 61 newsMessage.setToUserName(fromusername); 62 //開發者微信號 63 newsMessage.setFromUserName(tousername); 64 //消息建立時間 (整型) 65 newsMessage.setCreateTime(new Date().getTime()); 66 //消息類型news 67 newsMessage.setMsgType(MessageUtil.SEND_NEWS); 68 //圖文消息個數,限制爲10條之內 69 newsMessage.setArticleCount(newslist.size()); 70 //多條圖文消息信息,默認第一個item爲大圖 71 newsMessage.setArticles(newslist); 72 //轉成xml形式 73 respmessage = MessageUtil.newsMessageToXml(newsMessage); 74 //回覆信息 75 return respmessage; 76 } 77 }
打包
以上便是所要打的jar包包含的全部類,接下去講解打包:
由於項目中要將外部引用的jar包(dom4j-1.6.1.jar和xstream-1.3.1.jar)打進jar包,因此筆者準備了fat-jar(sourcefprge.net下的一個開源工具),下載地址爲http://pan.baidu.com/s/1CCtSf也能夠到http://sourcefprge.net/projects/fjep下載,將下載好的net.sf.fjep.fatjar_0.0.31.jar拷貝到eclipse目錄下的plugins目錄下,而後重啓eclipse,準備完成。
點擊項目wechatlib,右鍵選中Build Fat Jar
選擇須要用到的jar和文件
點擊finish便可
若是你急需用到wechatlib,可直接在此處下載http://pan.baidu.com/share/link?uk=1730904624&shareid=1479736624
轉帖請註明本文出自胖子的博客(http://www.cnblogs.com/Codenewbie),請尊重他人的辛勤勞動成果,謝謝!