微信公众号支付(java版)

来源:互联网 发布:电脑屏幕冷暖调节软件 编辑:程序博客网 时间:2024/06/06 07:02

package Le.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

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

import org.jdom2.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qq.connect.utils.http.HttpClient;

import Le.model.AliPayOrder;
import Le.service.IAliPayOrderService;
import Le.service.alipayService;
import cn.tenpay.configure.WxPayConfig;
import cn.tenpay.util.MD5Util;
import cn.tenpay.util.ServiceUtil;
import cn.tenpay.util.Sha1Util;
import cn.tenpay.util.StringToJsonUtil;
import cn.tenpay.util.XMLUtil;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(“/v_3”)
public class WxPayAction {
@Autowired
private IAliPayOrderService aliPayOrderService;
@Autowired
// private ILcMonthPayInfoService LcMonthPayInfoService;
private alipayService aliPayService;

/** * 微信客户端授权成功后根据redirect_uri参数调整到pay接口,去准备支付前信息接口 *  * @param request * @param response * @return * @throws Exception */// @ResponseBody@RequestMapping("pay")public String order(HttpServletRequest request, HttpServletResponse response) throws Exception {    /**     * 第一步:用户同意授权,根据参数,获取code     * 授权成功后返回的授权码,参考:http://mp.weixin.qq.com/wiki/17/     * c0f37d5704f0b64713d5d2c37b468d75.html#.E7.AC.AC.E4.B8.80.E6.AD.A5.EF.     * BC.9A.E7.94.A8.E6.88.B7.E5.90.8C.E6.84.8F.E6.8E.88.E6.9D.83.EF.BC.8C.     * E8.8E.B7.E5.8F.96code     */    String money = request.getParameter("money");// 支付钱 单位:分    String code = request.getParameter("code");// 用户授权吗    String state = request.getParameter("state");// 订单号获    String body = request.getParameter("body");// 商品信息    String attach = request.getParameter("attach");    System.out.println("attach.................................." + attach);    // state可以为任何含义,根据你前端需求,这里暂时叫商品id    // 授权码、商品id    System.out.println(            "code=" + code + ",state=" + state + ",moeny=" + money + ",body=" + body + ",attach." + attach);    /**     * 第二步:通过code换取网页授权access_token     * 根据授权码code获取access_token,参考:http://mp.weixin.qq.com/wiki/17/     * c0f37d5704f0b64713d5d2c37b468d75.html#.E7.AC.AC.E4.BA.8C.E6.AD.A5.EF.     * BC.9A.E9.80.9A.E8.BF.87code.E6.8D.A2.E5.8F.96.E7.BD.91.E9.A1.B5.E6.8E     * .88.E6.9D.83access_token     */    // 下面就到了获取openid,这个代表用户id.    // 获取openID    String openid = ServiceUtil.getOpenId(code);    // String openid = "oKSVcv7Z-IQm4RvAKHRxV8tIpAYI";    // 生成随机字符串    String noncestr = Sha1Util.getNonceStr();    // 生成1970年到现在的秒数.    String timestamp = Sha1Util.getTimeStamp();    // 订单号,自定义生成规则,只要全局唯一就OK    String out_trade_no = "NO" + System.currentTimeMillis() + "0001";    // 订单金额,    String order_price = String.valueOf(money); // 1分钱    // 商品描述,    // String body = "商品描述,测试....";    /**     * 第三步:统一下单接口     * 需要第二步生成的openid,参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?     * chapter=9_1     */    RequestHandler reqHandler = new RequestHandler(request, response);    // 初始化 RequestHandler 类可以在微信的文档中找到.还有相关工具类    reqHandler.init();    // 执行统一下单接口 获得预支付id,以下是必填参数    // 微信分配的公众账号ID(企业号corpid即为此appId)    reqHandler.setParameter("appid", WxPayConfig.APP_ID);    // 微信支付分配的商户号    reqHandler.setParameter("mch_id", WxPayConfig.MCH_ID);    // 终端设备号(门店号或收银设备ID),注意:PC网页或公众号内支付请传"WEB"    reqHandler.setParameter("device_info", "WEB");    // 随机字符串,不长于32位。推荐随机数生成算法    reqHandler.setParameter("nonce_str", noncestr);    // 商品描述    reqHandler.setParameter("body", body);    reqHandler.setParameter("attach", attach);    // 商家订单号    reqHandler.setParameter("out_trade_no", out_trade_no);    // 商品金额,以分为单位    reqHandler.setParameter("total_fee", order_price);    // APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。    reqHandler.setParameter("spbill_create_ip", "123.57.58.123");    // 下面的notify_url是用户支付成功后为微信调用的action 异步回调.    reqHandler.setParameter("notify_url", WxPayConfig.NOTIFY_URL);    // 交易类型,取值如下:JSAPI,NATIVE,APP,详细说明见参数规定    reqHandler.setParameter("trade_type", "JSAPI");    // ------------需要进行用户授权获取用户openid-------------    reqHandler.setParameter("openid", openid); // 这个必填.    /*     * <xml> <appid>wx242b1xc4370ec43b</appid> <attach>支付测试</attach>     * <body>JSAPI支付测试</body> <mch_id>10000100</mch_id>     * <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>     * <notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</     * notify_url> <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>     * <out_trade_no>1415659990</out_trade_no>     * <spbill_create_ip>14.23.150.211</spbill_create_ip>     * <total_fee>1</total_fee> <trade_type>JSAPI</trade_type>     * <sign>0CB0133B8C1EF10x3065174F50BCA001</sign> </xml>     */    // 生成签名,并且转为xml    String requestXml = reqHandler.getRequestXml();    System.out.println("requestXml:" + requestXml);    // 得到的预支付id    String prepay_id = ServiceUtil.unifiedorder(requestXml);    // Strinf str =    // Other.HttpRequest("https://api.mch.weixin.qq.com/pay/unifiedorder",    // "POST", requestXml);    SortedMap<String, String> params = new TreeMap<String, String>();    params.put("appId", WxPayConfig.APP_ID);    params.put("timeStamp", timestamp);    params.put("nonceStr", noncestr);    params.put("package", "prepay_id=" + prepay_id);    params.put("signType", "MD5");    System.out.println("params:" + JSONObject.fromObject(params).toString());    // 生成支付签名,这个签名 给 微信支付的调用使用    SortedMap<Object, Object> signMap = new TreeMap<Object, Object>();    signMap.put("appId", WxPayConfig.APP_ID);    signMap.put("timeStamp", timestamp);    signMap.put("nonceStr", noncestr);    signMap.put("package", "prepay_id=" + prepay_id);    signMap.put("signType", "MD5");    // 微信支付签名    String paySign = MD5Util.createSign(signMap, WxPayConfig.KEY);    System.out.println("PaySIGN:" + paySign);    // 微信分配的公众账号ID(企业号corpid即为此appId)    request.setAttribute("appId", WxPayConfig.APP_ID);    // 时间戳    request.setAttribute("timeStamp", timestamp);    // 随机字符串    request.setAttribute("nonceStr", noncestr);    // 预支付id ,就这样的格式    request.setAttribute("package", "prepay_id=" + prepay_id);    // 加密格式    request.setAttribute("signType", WxPayConfig.SIGN_TYPE);    // 微信支付签名e    request.setAttribute("paySign", paySign);    /*     * JSONObject map = new JSONObject(); map.put("appId",     * WxPayConfig.APP_ID); map.put("timeStamp", timestamp);     * map.put("nonceStr", noncestr); map.put("package", "prepay_id=" +     * prepay_id); map.put("signType", WxPayConfig.SIGN_TYPE);     * map.put("paySign", paySign); //将json 装入 session zhong     * request.getSession().setAttribute("json", map);     */    //    // String s = map.toString();    // System.out.println(s);    return "pay";    // return map;}/** * 异步返回 *  * @throws IOException * @throws ServletException * @throws ParseException */@RequestMapping("notify")public void notify(HttpServletRequest request, HttpServletResponse response)        throws ServletException, IOException, ParseException {    try {        InputStream inStream = request.getInputStream();        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len = 0;        while ((len = inStream.read(buffer)) != -1) {            outSteam.write(buffer, 0, len);        }        outSteam.close();        inStream.close();        String resultStr = new String(outSteam.toByteArray(), WxPayConfig.CHARTSET);        Map<String, String> resultMap = XMLUtil.doXMLParse(resultStr);        System.out.println("微信回调结果:" + resultMap.toString());        String is_subscribe = resultMap.get("is_subscribe");        System.out.println("is_subscribe...................." + is_subscribe);        String out_trade_no = resultMap.get("out_trade_no");        System.out.println("out_trade_no...................." + out_trade_no);        String transaction_id = resultMap.get("transaction_id");        System.out.println("订单号transaction_id.................." + transaction_id);        String sign = resultMap.get("sign");        System.out.println("sign....................." + sign);        String time_end = resultMap.get("time_end");        System.out.println("time_end....................." + time_end);        String bank_type = resultMap.get("bank_type");        System.out.println("bank_type....................." + bank_type);        String return_code = resultMap.get("return_code");        System.out.println("return_code....................." + return_code);        String attach = resultMap.get("attach");        System.out.println("attach====================" + attach);        if (attach == null) {            throw new RuntimeException("请求参数attach为空!");        }        if (return_code.equals("SUCCESS")) {            //操作数据库            boolean flag = aliPayService.modifyEleOrder(attach, transaction_id);            System.out.println("flag....................." + flag);            if (flag) {                // 通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.                response.getWriter().write(RequestHandler.setXML("SUCCESS", ""));                System.out.println("支付成功!SUCCESS");            } else {                response.getWriter().write(RequestHandler.setXML("FAIL", "数据操作错误!"));                System.out.println("支付成功!FAIL");            }        }        // request.setAttribute("out_trade_no", out_trade_no);    } catch (UnsupportedEncodingException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    } catch (JDOMException e) {        e.printStackTrace();    }}HttpClient client = new HttpClient();@RequestMapping(value = "redrect")public String httpClient(HttpServletRequest request, HttpServletResponse response) throws IOException {    String body = request.getParameter("body");    System.out.println("body值为:" + body);    String money = request.getParameter("money");    System.out.println("money值为:" + money);    String detail = request.getParameter("detail");    System.out.println("detail值为:" + detail);    String redirect = "http://www.xxxx.cn/xxx/v_3/pay.do?money=" + money + "&body=" + body + "&detail="            + detail;    System.out.println("redirect值为:" + redirect);    String encode_redirect = URLEncoder.encode(redirect, "GBK");    System.out.println(encode_redirect);    String str = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx89112dbccb52ef4c&redirect_uri="            + encode_redirect + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";    // 重定向到微信支付接口    return "redirect:" + str;

WePayConfig类
package cn.tenpay.configure;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class WxPayConfig {
// appid
public static String APP_ID = “111111111111111111111”;//你的appid
// JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
public static String APP_SECRET = “1111111111111111111111111”;//你的app_secret
// 受理商ID,身份标识
public static String MCH_ID = “111111111”;//必填
// 商户支付密钥Key,装完整数后,配置得到。

// 商户支付密钥Key,装完整数后,配置得到。32位长度public static String KEY = "888888888888888888888888888888";//你的Key

// public static String KEY = “”;
// 异步回调地址
public static String NOTIFY_URL = “http://www.xxxx.cn/xxxx/v_3/notify“;
// 字符编码
public static String CHARTSET = “UTF-8”;
// 加密方式
public static String SIGN_TYPE = “MD5”;
// redirect_uri,微信授权重定向地址
public static String REDIRECT_URI;

static {    try {        REDIRECT_URI = URLEncoder.encode("http://www.xxxx/xxx/_3/pay", CHARTSET);    } catch (UnsupportedEncodingException e) {        e.printStackTrace();    }}

}

}}

ServiceUtil类
package cn.tenpay.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

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

import cn.tenpay.configure.WxPayConfig;
import cn.tenpay.util.HttpClientUtil;
import cn.tenpay.util.XMLUtil;
import net.sf.json.JSONObject;

/**
* 自定义类,在官方文档中没有
*
*
*/
public class ServiceUtil {
private static final Log logger = LogFactory.getLog(ServiceUtil.class);

/** * 第二步:通过code换取网页授权access_token * 根据授权码code获取access_token,参考:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html#.E7.AC.AC.E4.BA.8C.E6.AD.A5.EF.BC.9A.E9.80.9A.E8.BF.87code.E6.8D.A2.E5.8F.96.E7.BD.91.E9.A1.B5.E6.8E.88.E6.9D.83access_token */public static String getOpenId(String code) {    String openParam = "appid=" + WxPayConfig.APP_ID + "&secret=" + WxPayConfig.APP_SECRET + "&code=" + code + "&grant_type=authorization_code";    String openJsonStr = HttpClientUtil.sendGET("https://api.weixin.qq.com/sns/oauth2/access_token", openParam);    System.out.println("openJsonStr:"+openJsonStr);    // 获取openid    JSONObject openJson = JSONObject.fromObject(openJsonStr);    String openid = openJson.getString("openid");    return openid;} /** * 统一下单接口 * 参考文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 * @return 预支付id * @throws Exception  */public static String unifiedorder(String requestXml) throws Exception{    // 统一下单接口提交 xml格式    URL orderUrl = new URL("https://api.mch.weixin.qq.com/pay/unifiedorder");    HttpURLConnection conn = (HttpURLConnection) orderUrl.openConnection();    conn.setConnectTimeout(30000); // 设置连接主机超时(单位:毫秒)    conn.setReadTimeout(30000); // 设置从主机读取数据超时(单位:毫秒)    conn.setDoOutput(true); // post请求参数要放在http正文内,顾设置成true,默认是false    conn.setDoInput(true); // 设置是否从httpUrlConnection读入,默认情况下是true    conn.setUseCaches(false); // Post 请求不能使用缓存    // 设定传送的内容类型是可序列化的java对象(如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");    conn.setRequestMethod("POST");// 设定请求的方法为"POST",默认是GET    conn.setRequestProperty("Content-Length", requestXml.length() + "");    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), WxPayConfig.CHARTSET);    out.write(requestXml.toString());    out.flush();    out.close();    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {        return null;    }    // 获取响应内容体    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), WxPayConfig.CHARTSET));    String line = "";    StringBuffer strBuf = new StringBuffer();    while ((line = in.readLine()) != null) {        strBuf.append(line).append("\n");    }    in.close();    System.out.println("result=========返回的xml=============\n" + strBuf.toString());    Map<String, String> orderMap = XMLUtil.doXMLParse(strBuf.toString());    System.out.println("orderMap===========================" + orderMap);    // 获取    String prepay_id = orderMap.get("prepay_id");    return prepay_id;}

}

MD5Util类
package cn.tenpay.util;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

import cn.tenpay.configure.WxPayConfig;

public class MD5Util {

@SuppressWarnings("rawtypes")public static String createSign(SortedMap<Object, Object> parameters, String key) {    StringBuffer sb = new StringBuffer();    Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序)    Iterator it = es.iterator();    while (it.hasNext()) {        Map.Entry entry = (Map.Entry) it.next();        String k = (String) entry.getKey();        Object v = entry.getValue();        if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {            sb.append(k + "=" + v + "&");        }    }    sb.append("key=" + key);    String sign = MD5Util.MD5Encode(sb.toString(), WxPayConfig.CHARTSET).toUpperCase();    return sign;}private static String byteArrayToHexString(byte b[]) {    StringBuffer resultSb = new StringBuffer();    for (int i = 0; i < b.length; i++)        resultSb.append(byteToHexString(b[i]));    return resultSb.toString();}private static String byteToHexString(byte b) {    int n = b;    if (n < 0)        n += 256;    int d1 = n / 16;    int d2 = n % 16;    return hexDigits[d1] + hexDigits[d2];}public static String MD5Encode(String origin, String charsetname) {    String resultString = null;    try {        resultString = new String(origin);        MessageDigest md = MessageDigest.getInstance("MD5");        if (charsetname == null || "".equals(charsetname))            resultString = byteArrayToHexString(md.digest(resultString.getBytes()));        else            resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));    } catch (Exception exception) {    }    return resultString;}private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",        "e", "f" };

}
XMLUtil类
package cn.tenpay.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.io.ByteArrayInputStream;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import cn.tenpay.configure.WxPayConfig;

/**
*
*
*/
public class XMLUtil {

/** * @param strxml * @return * @throws JDOMException * @throws IOException */public static Map doXMLParse(String strxml) throws JDOMException, IOException {    strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");    if(null == strxml || "".equals(strxml)) {        return null;    }    Map m = new HashMap();    InputStream in = new ByteArrayInputStream(strxml.getBytes(WxPayConfig.CHARTSET));    SAXBuilder builder = new SAXBuilder();    Document doc = builder.build(in);    Element root = doc.getRootElement();    List list = root.getChildren();    Iterator it = list.iterator();    while(it.hasNext()) {        Element e = (Element) it.next();        String k = e.getName();        String v = "";        List children = e.getChildren();        if(children.isEmpty()) {            v = e.getTextNormalize();        } else {            v = XMLUtil.getChildrenText(children);        }        m.put(k, v);    }    //�ر���    in.close();    return m;}/** * @param children * @return String */public static String getChildrenText(List children) {    StringBuffer sb = new StringBuffer();    if(!children.isEmpty()) {        Iterator it = children.iterator();        while(it.hasNext()) {            Element e = (Element) it.next();            String name = e.getName();            String value = e.getTextNormalize();            List list = e.getChildren();            sb.append("<" + name + ">");            if(!list.isEmpty()) {                sb.append(XMLUtil.getChildrenText(list));            }            sb.append(value);            sb.append("</" + name + ">");        }    }    return sb.toString();}/** * @param strxml * @return * @throws IOException  * @throws JDOMException  */public static String getXMLEncoding(String strxml) throws JDOMException, IOException {    InputStream in = HttpClientUtil.String2Inputstream(strxml);    SAXBuilder builder = new SAXBuilder();    Document doc = builder.build(in);    in.close();    return (String)doc.getProperty("encoding");}

}
HttpClientUtil类
package cn.tenpay.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import cn.tenpay.configure.WxPayConfig;

/**
*
*
*/
public class HttpClientUtil {

public static final String SunX509 = "SunX509";public static final String JKS = "JKS";public static final String PKCS12 = "PKCS12";public static final String TLS = "TLS";public static String sendGET(String url, String param) {    String result = "";// 访问返回结果    BufferedReader read = null;// 读取访问结果    try {        // 创建url        URL realurl = new URL(url + "?" + param);        // 打开连接        URLConnection connection = realurl.openConnection();        // 设置通用的请求属性        connection.setRequestProperty("accept", "*/*");        connection.setRequestProperty("connection", "Keep-Alive");        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");        // 建立连接        connection.connect();        // 获取所有响应头字段        // Map<String, List<String>> map = connection.getHeaderFields();        // 遍历所有的响应头字段,获取到cookies等        // for (String key : map.keySet()) {        // System.out.println(key + "--->" + map.get(key));        // }        // 定义 BufferedReader输入流来读取URL的响应        read = new BufferedReader(new InputStreamReader(connection.getInputStream(), WxPayConfig.CHARTSET));        String line;// 循环读取        while ((line = read.readLine()) != null) {            result += line;        }    } catch (IOException e) {        e.printStackTrace();    } finally {        if (read != null) {// 关闭流            try {                read.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    return result;}/** * get HttpURLConnection * @param strUrl url��ַ * @return HttpURLConnection * @throws IOException */public static HttpURLConnection getHttpURLConnection(String strUrl)        throws IOException {    URL url = new URL(strUrl);    HttpURLConnection httpURLConnection = (HttpURLConnection) url            .openConnection();    return httpURLConnection;}/** * get HttpsURLConnection * @param strUrl url��ַ * @return HttpsURLConnection * @throws IOException */public static HttpsURLConnection getHttpsURLConnection(String strUrl)        throws IOException {    URL url = new URL(strUrl);    HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url            .openConnection();    return httpsURLConnection;}/** * ��ȡ�����ѯ����url * @param strUrl * @return String */public static String getURL(String strUrl) {    if(null != strUrl) {        int indexOf = strUrl.indexOf("?");        if(-1 != indexOf) {            return strUrl.substring(0, indexOf);        }         return strUrl;    }    return strUrl;}/** * ��ȡ��ѯ�� * @param strUrl * @return String */public static String getQueryString(String strUrl) {    if(null != strUrl) {        int indexOf = strUrl.indexOf("?");        if(-1 != indexOf) {            return strUrl.substring(indexOf+1, strUrl.length());        }         return "";    }    return strUrl;}/** * ��ѯ�ַ�ת����Map<br/> * name1=key1&name2=key2&... * @param queryString * @return */public static Map queryString2Map(String queryString) {    if(null == queryString || "".equals(queryString)) {        return null;    }    Map m = new HashMap();    String[] strArray = queryString.split("&");    for(int index = 0; index < strArray.length; index++) {        String pair = strArray[index];        HttpClientUtil.putMapByPair(pair, m);    }    return m;}/** * �Ѽ�ֵ�����Map<br/> * pair:name=value * @param pair name=value * @param m */public static void putMapByPair(String pair, Map m) {    if(null == pair || "".equals(pair)) {        return;    }    int indexOf = pair.indexOf("=");    if(-1 != indexOf) {        String k = pair.substring(0, indexOf);        String v = pair.substring(indexOf+1, pair.length());        if(null != k && !"".equals(k)) {            m.put(k, v);        }    } else {        m.put(pair, "");    }}/** * BufferedReaderת����String<br/> * ע��:���ر���Ҫ���д��� * @param reader * @return String * @throws IOException */public static String bufferedReader2String(BufferedReader reader) throws IOException {    StringBuffer buf = new StringBuffer();    String line = null;    while( (line = reader.readLine()) != null) {        buf.append(line);        buf.append("\r\n");    }    return buf.toString();}/** * �������<br/> * ע��:���ر���Ҫ���д��� * @param out * @param data * @param len * @throws IOException */public static void doOutput(OutputStream out, byte[] data, int len)        throws IOException {    int dataLen = data.length;    int off = 0;    while (off < data.length) {        if (len >= dataLen) {            out.write(data, off, dataLen);            off += dataLen;        } else {            out.write(data, off, len);            off += len;            dataLen -= len;        }        // ˢ�»�����        out.flush();    }}/** * ��ȡSSLContext * @param trustFile  * @param trustPasswd * @param keyFile * @param keyPasswd * @return * @throws NoSuchAlgorithmException  * @throws KeyStoreException  * @throws IOException  * @throws CertificateException  * @throws UnrecoverableKeyException  * @throws KeyManagementException  */public static SSLContext getSSLContext(        FileInputStream trustFileInputStream, String trustPasswd,        FileInputStream keyFileInputStream, String keyPasswd)        throws NoSuchAlgorithmException, KeyStoreException,        CertificateException, IOException, UnrecoverableKeyException,        KeyManagementException {    // ca    TrustManagerFactory tmf = TrustManagerFactory.getInstance(HttpClientUtil.SunX509);    KeyStore trustKeyStore = KeyStore.getInstance(HttpClientUtil.JKS);    trustKeyStore.load(trustFileInputStream, HttpClientUtil            .str2CharArray(trustPasswd));    tmf.init(trustKeyStore);    final char[] kp = HttpClientUtil.str2CharArray(keyPasswd);    KeyManagerFactory kmf = KeyManagerFactory.getInstance(HttpClientUtil.SunX509);    KeyStore ks = KeyStore.getInstance(HttpClientUtil.PKCS12);    ks.load(keyFileInputStream, kp);    kmf.init(ks, kp);    SecureRandom rand = new SecureRandom();    SSLContext ctx = SSLContext.getInstance(HttpClientUtil.TLS);    ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), rand);    return ctx;}/** * ��ȡCA֤����Ϣ * @param cafile CA֤���ļ� * @return Certificate * @throws CertificateException * @throws IOException */public static Certificate getCertificate(File cafile)        throws CertificateException, IOException {    CertificateFactory cf = CertificateFactory.getInstance("X.509");    FileInputStream in = new FileInputStream(cafile);    Certificate cert = cf.generateCertificate(in);    in.close();    return cert;}/** * �ַ�ת����char���� * @param str * @return char[] */public static char[] str2CharArray(String str) {    if(null == str) return null;    return str.toCharArray();}/** * �洢ca֤���JKS��ʽ * @param cert * @param alias * @param password * @param out * @throws KeyStoreException * @throws NoSuchAlgorithmException * @throws CertificateException * @throws IOException */public static void storeCACert(Certificate cert, String alias,        String password, OutputStream out) throws KeyStoreException,        NoSuchAlgorithmException, CertificateException, IOException {    KeyStore ks = KeyStore.getInstance("JKS");    ks.load(null, null);    ks.setCertificateEntry(alias, cert);    // store keystore    ks.store(out, HttpClientUtil.str2CharArray(password));}public static InputStream String2Inputstream(String str) {    return new ByteArrayInputStream(str.getBytes());}/** * InputStreamת����Byte * ע��:���ر���Ҫ���д��� * @param in * @return byte * @throws Exception */public static byte[] InputStreamTOByte(InputStream in) throws IOException{      int BUFFER_SIZE = 4096;      ByteArrayOutputStream outStream = new ByteArrayOutputStream();     byte[] data = new byte[BUFFER_SIZE];      int count = -1;      while((count = in.read(data,0,BUFFER_SIZE)) != -1)          outStream.write(data, 0, count);      data = null;      byte[] outByte = outStream.toByteArray();    outStream.close();    return outByte;  } /** * InputStreamת����String * ע��:���ر���Ҫ���д��� * @param in * @param encoding ���� * @return String * @throws Exception */public static String InputStreamTOString(InputStream in,String encoding) throws IOException{      return new String(InputStreamTOByte(in),encoding);}

}

支付页面展示

这里写图片描述

原创粉丝点击