JAVA微信開發總結的圖文代碼分享

黄舟
發布: 2017-04-15 11:17:19
原創
1712 人瀏覽過

最近研究了一下微信,感覺不是很難,借鑒了marker(www.yl-blog.com)的開源程式碼,修改了幾處小BUG(在作者的開源網站上留了言,詳見其開源網站),很快就實現關鍵的邏輯: 
  1、開發者驗證 
  2、透過程式介面自訂選單 
  3、使用者追蹤後,發送歡迎訊息 
 4、根據使用者追蹤輸入為其選擇感興趣的主題 
  5、發送文件訊息、發送圖文訊息 
  6、使用靜態網頁模擬了微信網站的簡單模板 

  公眾號,daydayup_it,正在緊張的策劃開發中,近期上線,計畫主要提供一些優質的教育資源,敬請大家關注。 

  準備有空把關鍵技術整理一下,貼出來跟大家交流。

  一、開發者驗證,其實主要是寫個Servlet,有點Web開發經驗就很容易搞定 

package org.marker.weixin.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 处理微信服务器请求的Servlet URL地址:http://xxx/weixin/dealwith.do
 * 
 * @author marker
 * @blog www.yl-blog.com
 * @weibo http://t.qq.com/wuweiit
 */
public class WinXinServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(WinXinServlet.class);

	// TOKEN 是你在微信平台开发模式中设置的字符串
	public static final String TOKEN = "YourToken";

	/**
	 * 处理微信服务器验证
	 * http://wallimn.iteye.com, 2014-09-11
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String signature = request.getParameter("signature");// 微信加密签名
		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");// 随机数
		String echostr = request.getParameter("echostr");// 随机字符串
		Writer out = response.getWriter();
		System.out.println("收到验证请求:");
		System.out.println("  signature="+signature);
		System.out.println("  timestamp="+timestamp);
		System.out.println("  nonce="+nonce);
		System.out.println("  echostr="+echostr);
		if(signature==null || timestamp==null || nonce==null || echostr==null){
			//这几个参数为空时,排序会报错。
			out.write("parameter is null!");
		}
		else{
			// 重写totring方法,得到三个参数的拼接字符串
			List<String> list = new ArrayList<String>(3) {
				private static final long serialVersionUID = 2621444383666420433L;
				public String toString() {
					return this.get(0) + this.get(1) + this.get(2);
				}
			};
			list.add(TOKEN);
			list.add(timestamp);
			list.add(nonce);
			Collections.sort(list);// 排序
			String tmpStr = new MySecurity().encode(list.toString(),
					MySecurity.SHA_1);// SHA-1加密
			if (signature.equals(tmpStr)) {
				out.write(echostr);// 请求验证成功,返回随机码
			} else {
				out.write("check error!");
			}
		}
		out.flush();
		out.close();
	}

	
	/**
	 * 处理微信服务器发过来的各种消息,包括:文本、图片、地理位置、音乐等等
	 * http://wallimn.iteye.com, 2014-09-11
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info("收到POST请求:"+(new Date()));
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		InputStream is = request.getInputStream();
		OutputStream os = response.getOutputStream();
		//TODO:写微信平台推送过来的各种信息的处理逻辑
	}
}
登入後複製

  二、微信網站的測試主頁效果及程式碼,借助bootstrap,支援媒體查詢,當手機水平看時,欄會變成2*2佈局,index.jsp,需要JSTL的支持,也可以自行去掉,不影響效果,注意連接文件的路徑即可 

JAVA微信開發總結的圖文代碼分享

JAVA微信開發總結的圖文代碼分享


#

以上是JAVA微信開發總結的圖文代碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!