| | |
| | | package org.yeshi.utils.wx;
|
| | |
|
| | | import java.io.ByteArrayInputStream;
|
| | | import java.io.InputStream;
|
| | | import java.util.HashMap;
|
| | | import java.util.Iterator;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | | import java.util.TreeMap;
|
| | |
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | | import org.dom4j.Document;
|
| | | import org.dom4j.Element;
|
| | | import org.dom4j.io.SAXReader;
|
| | | import org.yeshi.utils.HttpUtil;
|
| | | import org.yeshi.utils.StringUtil;
|
| | |
|
| | | import com.qq.weixin.mp.aes.AesException;
|
| | | import com.qq.weixin.mp.aes.WXBizMsgCrypt;
|
| | |
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class WXUtil {
|
| | |
|
| | | public static String getContent(HttpServletRequest req) {
|
| | | try {
|
| | | byte[] buffer = new byte[2048];
|
| | | int readBytes = 0;
|
| | | StringBuilder stringBuilder = new StringBuilder();
|
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) {
|
| | | stringBuilder.append(new String(buffer, 0, readBytes));
|
| | | }
|
| | | return stringBuilder.toString();
|
| | | } catch (Exception e) {
|
| | |
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | @SuppressWarnings("unchecked")
|
| | | public static Map<String, String> parseXML(HttpServletRequest req) {
|
| | |
|
| | | SAXReader reader = new SAXReader();
|
| | | try {
|
| | | byte[] buffer = new byte[2048];
|
| | | int readBytes = 0;
|
| | | StringBuilder stringBuilder = new StringBuilder();
|
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) {
|
| | | stringBuilder.append(new String(buffer, 0, readBytes));
|
| | | }
|
| | | InputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes());
|
| | | Map<String, String> map = new HashMap<String, String>();
|
| | | Document document = reader.read(inputStream);
|
| | | Element root = document.getRootElement();
|
| | | // 得到根元素的所有子节点
|
| | | List<Element> elementList = root.elements();
|
| | | // 遍历所有子节点
|
| | | for (Element e : elementList)
|
| | | map.put(e.getName(), e.getText());
|
| | | inputStream.close();
|
| | | inputStream = null;
|
| | | return map;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | // 获取微信回调的内容
|
| | | public static String getWXXMLBody(HttpServletRequest req) {
|
| | | byte[] buffer = new byte[2048];
|
| | | int readBytes = 0;
|
| | | StringBuilder stringBuilder = new StringBuilder();
|
| | | try {
|
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) {
|
| | | stringBuilder.append(new String(buffer, 0, readBytes));
|
| | | }
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return stringBuilder.toString();
|
| | | }
|
| | |
|
| | | @SuppressWarnings("unchecked")
|
| | | public static Map<String, String> parseXML(String content, String msgSignature, String timeStamp, String nonce,
|
| | | String token, String encodingAesKey, String appId) {
|
| | | SAXReader reader = new SAXReader();
|
| | | try {
|
| | | String result = "";
|
| | | try {
|
| | | WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
|
| | | result = pc.DecryptMsg(msgSignature, timeStamp, nonce, content);
|
| | | } catch (AesException e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | |
|
| | | InputStream inputStream = new ByteArrayInputStream(result.getBytes());
|
| | | Map<String, String> map = new HashMap<String, String>();
|
| | | Document document = reader.read(inputStream);
|
| | | Element root = document.getRootElement();
|
| | | // 得到根元素的所有子节点
|
| | | List<Element> elementList = root.elements();
|
| | | // 遍历所有子节点
|
| | | for (Element e : elementList)
|
| | | map.put(e.getName(), e.getText());
|
| | | inputStream.close();
|
| | | inputStream = null;
|
| | |
|
| | | return map;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | @SuppressWarnings("unchecked")
|
| | | public static Map<String, String> parseXML(HttpServletRequest req, String pwd) {
|
| | | SAXReader reader = new SAXReader();
|
| | | try {
|
| | | byte[] buffer = new byte[2048];
|
| | | int readBytes = 0;
|
| | | StringBuilder stringBuilder = new StringBuilder();
|
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) {
|
| | | stringBuilder.append(new String(buffer, 0, readBytes));
|
| | | }
|
| | | System.out.println(stringBuilder.toString());
|
| | | InputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes());
|
| | | Map<String, String> map = new HashMap<String, String>();
|
| | | Document document = reader.read(inputStream);
|
| | | Element root = document.getRootElement();
|
| | | // 得到根元素的所有子节点
|
| | | List<Element> elementList = root.elements();
|
| | | // 遍历所有子节点
|
| | | for (Element e : elementList)
|
| | | map.put(e.getName(), e.getText());
|
| | | inputStream.close();
|
| | | inputStream = null;
|
| | | return map;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return null;
|
| | | }
|
| | |
|
| | | @SuppressWarnings("unchecked")
|
| | | public static Map<String, String> parseXML(String data) {
|
| | | SAXReader reader = new SAXReader();
|
| | | try {
|
| | | InputStream inputStream = new ByteArrayInputStream(data.getBytes("UTF-8"));
|
| | | Map<String, String> map = new HashMap<String, String>();
|
| | | Document document = reader.read(inputStream);
|
| | | Element root = document.getRootElement();
|
| | | // 得到根元素的所有子节点
|
| | | List<Element> elementList = root.elements();
|
| | | // 遍历所有子节点
|
| | | for (Element e : elementList)
|
| | | map.put(e.getName(), e.getText());
|
| | | inputStream.close();
|
| | | inputStream = null;
|
| | | return map;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return null;
|
| | | }
|
| | |
|
| | | public static String loadWXMessage(Map<String, String> map) {
|
| | | Set<String> set = map.keySet();
|
| | | Iterator<String> its = set.iterator();
|
| | | String xml = "<xml>";
|
| | | while (its.hasNext()) {
|
| | | String key = its.next();
|
| | | String value = map.get(key);
|
| | | xml += String.format("<%1$s><![CDATA[%2$s]]></%3$s>", key, value, key);
|
| | | }
|
| | | xml += "</xml>";
|
| | | return xml;
|
| | | }
|
| | |
|
| | | public static String getSignMD5(Map<String, String> map, String k) {
|
| | | Map<String, String> tm = new TreeMap<String, String>();
|
| | | tm.putAll(map);
|
| | | Set<String> set = tm.keySet();
|
| | | Iterator<String> its = set.iterator();
|
| | | String result = "";
|
| | | while (its.hasNext()) {
|
| | | String key = its.next();
|
| | | String val = tm.get(key);
|
| | | result += key + "=" + val + "&";
|
| | | }
|
| | | result += "key=" + k;
|
| | | return StringUtil.Md5(result).toUpperCase();
|
| | | }
|
| | |
|
| | | public static String getAcessToken(String appId, String appSecret) {
|
| | | String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId
|
| | | + "&secret=" + appSecret;
|
| | | String token = HttpUtil.get(tokenUrl);
|
| | | String access_token = JSONObject.fromObject(token).optString("access_token");
|
| | | return access_token;
|
| | | }
|
| | |
|
| | | // 获取详细的用户信息
|
| | | public static JSONObject getDetailGZUserInfo(String openId, String accessToken) {
|
| | | String url = String.format("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN",
|
| | | accessToken, openId);
|
| | | String result = HttpUtil.get(url);
|
| | | return JSONObject.fromObject(result);
|
| | | }
|
| | |
|
| | | // 创建菜单
|
| | | public static void createMenu(String accessToken, JSONObject root) {
|
| | | HttpUtil.post("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken);
|
| | | // 创建菜单再
|
| | | String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
|
| | | String result = HttpUtil.post(url, root.toString());
|
| | | }
|
| | |
|
| | | }
|
| | | package org.yeshi.utils.wx; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.TreeMap; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import org.dom4j.Document; |
| | | import org.dom4j.Element; |
| | | import org.dom4j.io.SAXReader; |
| | | import org.yeshi.utils.HttpUtil; |
| | | import org.yeshi.utils.StringUtil; |
| | | |
| | | import com.qq.weixin.mp.aes.AesException; |
| | | import com.qq.weixin.mp.aes.WXBizMsgCrypt; |
| | | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | public class WXUtil { |
| | | |
| | | public static String getContent(HttpServletRequest req) { |
| | | try { |
| | | byte[] buffer = new byte[2048]; |
| | | int readBytes = 0; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) { |
| | | stringBuilder.append(new String(buffer, 0, readBytes)); |
| | | } |
| | | return stringBuilder.toString(); |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static Map<String, String> parseXML(HttpServletRequest req) { |
| | | |
| | | SAXReader reader = new SAXReader(); |
| | | try { |
| | | byte[] buffer = new byte[2048]; |
| | | int readBytes = 0; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) { |
| | | stringBuilder.append(new String(buffer, 0, readBytes)); |
| | | } |
| | | InputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes()); |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | Document document = reader.read(inputStream); |
| | | Element root = document.getRootElement(); |
| | | // 得到根元素的所有子节点 |
| | | List<Element> elementList = root.elements(); |
| | | // 遍历所有子节点 |
| | | for (Element e : elementList) |
| | | map.put(e.getName(), e.getText()); |
| | | inputStream.close(); |
| | | inputStream = null; |
| | | return map; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | // 获取微信回调的内容 |
| | | public static String getWXXMLBody(HttpServletRequest req) { |
| | | byte[] buffer = new byte[2048]; |
| | | int readBytes = 0; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | try { |
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) { |
| | | stringBuilder.append(new String(buffer, 0, readBytes)); |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | return stringBuilder.toString(); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static Map<String, String> parseXML(String content, String msgSignature, String timeStamp, String nonce, |
| | | String token, String encodingAesKey, String appId) { |
| | | SAXReader reader = new SAXReader(); |
| | | try { |
| | | String result = ""; |
| | | try { |
| | | WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId); |
| | | result = pc.DecryptMsg(msgSignature, timeStamp, nonce, content); |
| | | } catch (AesException e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | |
| | | InputStream inputStream = new ByteArrayInputStream(result.getBytes()); |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | Document document = reader.read(inputStream); |
| | | Element root = document.getRootElement(); |
| | | // 得到根元素的所有子节点 |
| | | List<Element> elementList = root.elements(); |
| | | // 遍历所有子节点 |
| | | for (Element e : elementList) |
| | | map.put(e.getName(), e.getText()); |
| | | inputStream.close(); |
| | | inputStream = null; |
| | | |
| | | return map; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static Map<String, String> parseXML(HttpServletRequest req, String pwd) { |
| | | SAXReader reader = new SAXReader(); |
| | | try { |
| | | byte[] buffer = new byte[2048]; |
| | | int readBytes = 0; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | while ((readBytes = req.getInputStream().read(buffer)) > 0) { |
| | | stringBuilder.append(new String(buffer, 0, readBytes)); |
| | | } |
| | | System.out.println(stringBuilder.toString()); |
| | | InputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes()); |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | Document document = reader.read(inputStream); |
| | | Element root = document.getRootElement(); |
| | | // 得到根元素的所有子节点 |
| | | List<Element> elementList = root.elements(); |
| | | // 遍历所有子节点 |
| | | for (Element e : elementList) |
| | | map.put(e.getName(), e.getText()); |
| | | inputStream.close(); |
| | | inputStream = null; |
| | | return map; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static Map<String, String> parseXML(String data) { |
| | | SAXReader reader = new SAXReader(); |
| | | try { |
| | | InputStream inputStream = new ByteArrayInputStream(data.getBytes("UTF-8")); |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | Document document = reader.read(inputStream); |
| | | Element root = document.getRootElement(); |
| | | // 得到根元素的所有子节点 |
| | | List<Element> elementList = root.elements(); |
| | | // 遍历所有子节点 |
| | | for (Element e : elementList) |
| | | map.put(e.getName(), e.getText()); |
| | | inputStream.close(); |
| | | inputStream = null; |
| | | return map; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | public static String loadWXMessage(Map<String, String> map) { |
| | | Set<String> set = map.keySet(); |
| | | Iterator<String> its = set.iterator(); |
| | | String xml = "<xml>"; |
| | | while (its.hasNext()) { |
| | | String key = its.next(); |
| | | String value = map.get(key); |
| | | xml += String.format("<%1$s><![CDATA[%2$s]]></%3$s>", key, value, key); |
| | | } |
| | | xml += "</xml>"; |
| | | return xml; |
| | | } |
| | | |
| | | public static String getSignMD5(Map<String, String> map, String k) { |
| | | Map<String, String> tm = new TreeMap<String, String>(); |
| | | tm.putAll(map); |
| | | Set<String> set = tm.keySet(); |
| | | Iterator<String> its = set.iterator(); |
| | | String result = ""; |
| | | while (its.hasNext()) { |
| | | String key = its.next(); |
| | | String val = tm.get(key); |
| | | result += key + "=" + val + "&"; |
| | | } |
| | | result += "key=" + k; |
| | | return StringUtil.Md5(result).toUpperCase(); |
| | | } |
| | | |
| | | public static String getAcessToken(String appId, String appSecret) { |
| | | String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId |
| | | + "&secret=" + appSecret; |
| | | String token = HttpUtil.get(tokenUrl); |
| | | String access_token = JSONObject.fromObject(token).optString("access_token"); |
| | | return access_token; |
| | | } |
| | | |
| | | // 获取详细的用户信息 |
| | | public static JSONObject getDetailGZUserInfo(String openId, String accessToken) { |
| | | String url = String.format("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN", |
| | | accessToken, openId); |
| | | String result = HttpUtil.get(url); |
| | | return JSONObject.fromObject(result); |
| | | } |
| | | |
| | | // 创建菜单 |
| | | public static void createMenu(String accessToken, JSONObject root) { |
| | | HttpUtil.post("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken); |
| | | // 创建菜单再 |
| | | String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken; |
| | | String result = HttpUtil.post(url, root.toString()); |
| | | } |
| | | |
| | | public static String getShortUrl(String accessToken, String url) { |
| | | |
| | | String requestUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" + accessToken; |
| | | JSONObject data = new JSONObject(); |
| | | data.put("action", "long2short"); |
| | | data.put("long_url", url); |
| | | String result = HttpUtil.post(requestUrl, data.toString()); |
| | | System.out.println(result); |
| | | return ""; |
| | | } |
| | | |
| | | } |