休息食客

          隨心而動(dòng)

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            16 隨筆 :: 7 文章 :: 2 評(píng)論 :: 0 Trackbacks

          2015年9月15日 #

          當(dāng)請(qǐng)求結(jié)束后,會(huì)受到微信服務(wù)器返回給我們的結(jié)果
          處理第三節(jié)中的reback
          //將接收到的返回信息
            Map<String,Object> mso = new HashMap<String,Object>();
             mso = XMLParser.getMapFromXML(reback);
          需要抓取異常
          寫一個(gè)XMLParser類
          import org.w3c.dom.Document;
          import org.w3c.dom.Element;
          import org.w3c.dom.Node;
          import org.w3c.dom.NodeList;
          import org.xml.sax.SAXException;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.ParserConfigurationException;
          import java.io.IOException;
          import java.io.InputStream;
          import java.util.HashMap;
          import java.util.Map;
          /**
           * User: rizenguo
           * Date: 2014/11/1
           * Time: 14:06
           */
          public class XMLParser {
              public static Map<String,Object> getMapFromXML(String xmlString) throws ParserConfigurationException, IOException, SAXException {
                  //這里用Dom的方式解析回包的最主要目的是防止API新增回包字段
                  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder builder = factory.newDocumentBuilder();
                  InputStream is =  Util.getStringStream(xmlString);
                  Document document = builder.parse(is);
                  //獲取到document里面的全部結(jié)點(diǎn)
                  NodeList allNodes = document.getFirstChild().getChildNodes();
                  Node node;
                  Map<String, Object> map = new HashMap<String, Object>();
                  int i=0;
                  while (i < allNodes.getLength()) {
                      node = allNodes.item(i);
                      if(node instanceof Element){
                          map.put(node.getNodeName(),node.getTextContent());
                      }
                      i++;
                  }
                  return map;
              }
             
          }
          好吧,到這里,已經(jīng)將結(jié)果都放入一個(gè)map里面去了,,筆者自己寫了一個(gè)返回類,把map里的結(jié)果都set到返回類中,
          最終都返回給調(diào)用這個(gè)現(xiàn)金紅包接口的其他業(yè)務(wù)。
          大家可以參考一下
          寫一個(gè)WechatCashHongbaoSendRsp類
          public class WechatCashHongbaoSendRsp {
           
           private String return_code;                 //返回狀態(tài)碼
           private String return_msg;                  //返回信息
           
           //以下字段在return_code為SUCCESS的時(shí)候有返回
           private String sign;                        //簽名
           private String result_code;                 //業(yè)務(wù)結(jié)果
           private String err_code;                    //錯(cuò)誤代碼
           private String err_code_des;                //錯(cuò)誤代碼描述
           
           //以下字段在return_code和result_code都為SUCCESS的時(shí)候有返回
           private String mch_billno;                  //商戶訂單號(hào)
           private String mch_id;                      //商戶號(hào)
           private String wxappid;                     //公眾賬號(hào)appid
           private String re_openid;                   //用戶openid
           private String total_amount;                //付款金額
           private String send_time;                   //發(fā)放成功時(shí)間
           private String send_listid;                 //微信單號(hào)
           
           public String getReturn_code() {
            return return_code;
           }
           public void setReturn_code(String return_code) {
            this.return_code = return_code;
           }
           public String getReturn_msg() {
            return return_msg;
           }
           public void setReturn_msg(String return_msg) {
            this.return_msg = return_msg;
           }
           public String getSign() {
            return sign;
           }
           public void setSign(String sign) {
            this.sign = sign;
           }
           public String getResult_code() {
            return result_code;
           }
           public void setResult_code(String result_code) {
            this.result_code = result_code;
           }
           public String getErr_code() {
            return err_code;
           }
           public void setErr_code(String err_code) {
            this.err_code = err_code;
           }
           public String getErr_code_des() {
            return err_code_des;
           }
           public void setErr_code_des(String err_code_des) {
            this.err_code_des = err_code_des;
           }
           public String getMch_billno() {
            return mch_billno;
           }
           public void setMch_billno(String mch_billno) {
            this.mch_billno = mch_billno;
           }
           public String getMch_id() {
            return mch_id;
           }
           public void setMch_id(String mch_id) {
            this.mch_id = mch_id;
           }
           public String getWxappid() {
            return wxappid;
           }
           public void setWxappid(String wxappid) {
            this.wxappid = wxappid;
           }
           public String getRe_openid() {
            return re_openid;
           }
           public void setRe_openid(String re_openid) {
            this.re_openid = re_openid;
           }
           public String getTotal_amount() {
            return total_amount;
           }
           public void setTotal_amount(String total_amount) {
            this.total_amount = total_amount;
           }
           public String getSend_time() {
            return send_time;
           }
           public void setSend_time(String send_time) {
            this.send_time = send_time;
           }
           public String getSend_listid() {
            return send_listid;
           }
           public void setSend_listid(String send_listid) {
            this.send_listid = send_listid;
           }
          }
          將map里的值都set進(jìn)來(lái)
          WechatCashHongbaoSendRsp rsp = new WechatCashHongbaoSendRsp();
            rsp.setReturn_code(String.valueOf(mso.get("return_code")));
            rsp.setReturn_msg(String.valueOf(mso.get("return_msg")));
           
            //以下字段在return_code為SUCCESS的時(shí)候有返回
            if(String.valueOf(mso.get("return_code")).equals("SUCCESS")){
             rsp.setSign(String.valueOf(mso.get("sign")));
             rsp.setResult_code(String.valueOf(mso.get("result_code")));
             rsp.setErr_code(String.valueOf(mso.get("err_code")));
             rsp.setErr_code_des(String.valueOf(mso.get("err_code_des")));
             
             //以下字段在return_code為SUCCESS的時(shí)候有返回
             if(String.valueOf(mso.get("result_code")).equals("SUCCESS")) {
              rsp.setMch_billno(String.valueOf(mso.get("mch_billno")));
              rsp.setMch_id(String.valueOf(mso.get("mch_id")));
              rsp.setWxappid(String.valueOf(mso.get("wxappid")));
              rsp.setRe_openid(String.valueOf(mso.get("re_openid")));
              rsp.setTotal_amount(String.valueOf(mso.get("total_amount")));
              rsp.setSend_time(String.valueOf(mso.get("send_time")));
              rsp.setSend_listid(String.valueOf(mso.get("send_listid")));
             }
            }
          將此結(jié)果返回出去吧!
          聲明:工具類代碼參考微信支付開發(fā)的demo,有些自己做了小改動(dòng)。

          最后說(shuō)一點(diǎn)點(diǎn),開發(fā)這個(gè)應(yīng)用接口,除了在加載證書那里遇到困難,其他地方還是比較順利的,然后筆者寫這篇博客,希望將
          自己的研究的東西分享出去,有哪些不對(duì)的地方,或者不好的地方,請(qǐng)指教,畢竟當(dāng)局者迷旁觀者清。
          posted @ 2015-09-15 11:44 休息食客 閱讀(308) | 評(píng)論 (0)編輯 收藏

          2015年9月14日 #

          當(dāng)把要發(fā)送的數(shù)據(jù)準(zhǔn)備好之后,接下來(lái)是要寫https請(qǐng)求,把它發(fā)送給微信服務(wù)器
          String reback = HttpClientUtil.sendHttpsUrl("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", data);
          需要抓取異常,并處理好異常
          寫一個(gè)HttpClientUtil類
          import java.io.BufferedReader;
          import java.io.ByteArrayInputStream;
          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.io.OutputStreamWriter;
          import java.net.HttpURLConnection;
          import java.net.URL;
          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.security.cert.X509Certificate;
          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 com.yxht.core.common.tools.LoadProperties;
          import com.yxht.core.modules.WechatPay.client.TenpayHttpClient;
          /**
           * Http客戶端工具類<br/>
           * 這是內(nèi)部調(diào)用類,請(qǐng)不要在外部調(diào)用。
           * @author miklchen
           *
           */
          public class HttpClientUtil {
           
           /**
            * 發(fā)送請(qǐng)求(微信支付用,發(fā)紅包等等)
            * @param url
            * @param params
            * @return
            * @throws Exception
            */
           public static String sendHttpsUrl(String sendUrl, String params) throws Exception{
            String jsonStr = "";
            String certificate_path = "d\:\\rootca.pem"; //信任庫(kù)證書
            String keystore_path = "d\:\\apiclient_cert.p12"; //密鑰庫(kù)證書
            String password = "xxxxxxxx"; //商戶號(hào),也是加載證書的密碼
           
            // 聲明SSL上下文  
                  SSLContext sslContext = null;  
                  // 實(shí)例化主機(jī)名驗(yàn)證接口  
                  sslContext = getSSLContext(password, keystore_path, certificate_path);  
                  if (sslContext != null) {  
                      HttpsURLConnection.setDefaultSSLSocketFactory(sslContext  
                              .getSocketFactory());  
                  }  
            URL url = new URL(sendUrl);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            //設(shè)置套接工廠
            con.setSSLSocketFactory(sslContext.getSocketFactory());
            OutputStreamWriter writer = new OutputStreamWriter(
              con.getOutputStream(), "UTF-8");
            writer.write(params);
            writer.flush();
            writer.close();
            InputStream in = con.getInputStream();
            BufferedReader db = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String tmp = "";
            while ((tmp = db.readLine()) != null) {
             jsonStr += tmp;
            }
            return jsonStr;
           
           }
           
           /**
               * 獲得SSLSocketFactory.
               * @param password
               *            密碼
               * @param keyStorePath
               *            密鑰庫(kù)路徑
               * @param trustStorePath
               *            信任庫(kù)路徑
               * @return SSLSocketFactory
               * @throws Exception
               */  
              public static SSLContext getSSLContext(String password,  
                      String keyStorePath, String trustStorePath) throws Exception {  
                  // 實(shí)例化密鑰庫(kù)  
                  KeyManagerFactory keyManagerFactory = KeyManagerFactory  
                          .getInstance(KeyManagerFactory.getDefaultAlgorithm());  
                  // 獲得密鑰庫(kù)  
                  KeyStore keyStore = getKeyStore(password, keyStorePath);  
                  // 初始化密鑰工廠  
                  keyManagerFactory.init(keyStore, password.toCharArray());  
           
                  // 實(shí)例化信任庫(kù)  
          //        TrustManagerFactory trustManagerFactory = TrustManagerFactory  
          //                .getInstance(TrustManagerFactory.getDefaultAlgorithm());  
          //        // 獲得信任庫(kù)  
          //        KeyStore trustStore = getKeyStore(password, trustStorePath);  
          //        // 初始化信任庫(kù)  
          //        trustManagerFactory.init(trustStore);  
                  // 實(shí)例化SSL上下文  
                  SSLContext ctx = SSLContext.getInstance("TLS");  
                  // 初始化SSL上下文  
                  ctx.init(keyManagerFactory.getKeyManagers(),  
                          null, null);  
                  // 獲得SSLSocketFactory  
                  return ctx;  
              }  
             
              /**
               * 獲得KeyStore.
               * @param keyStorePath
               *            密鑰庫(kù)路徑
               * @param password
               *            密碼
               * @return 密鑰庫(kù)
               * @throws Exception
               */  
              public static KeyStore getKeyStore(String password, String keyStorePath)  
                      throws Exception {  
                  // 實(shí)例化密鑰庫(kù)  
                  KeyStore ks = KeyStore.getInstance("PKCS12");  
                  // 獲得密鑰庫(kù)文件流  
                  FileInputStream is = new FileInputStream(keyStorePath);  
                  // 加載密鑰庫(kù)  
                  ks.load(is, password.toCharArray());  
                  // 關(guān)閉密鑰庫(kù)文件流  
                  is.close();  
                  return ks;  
              }  
          }
          有些import的沒(méi)用,可以刪掉,只要程序代碼沒(méi)有報(bào)錯(cuò)的地方就行。
          到這里,如果發(fā)送請(qǐng)求,會(huì)失敗,它會(huì)說(shuō)你沒(méi)有安裝證書,到商戶平臺(tái)上去下載證書,文檔里有說(shuō)明
          下載后一共有4個(gè)證書,至少需要密鑰庫(kù)證書apiclient_cert.p12,因?yàn)樾湃螏?kù)證書rootca.pem我不會(huì)用(哪位大神會(huì)用教教我),
          文檔里也說(shuō)了信任庫(kù)證書不用也行,證書安裝請(qǐng)仔細(xì)研究文檔,代碼上面已經(jīng)寫好了。

          最后最后,,請(qǐng)發(fā)送請(qǐng)求吧!!!

          聲明:工具類代碼參考微信支付開發(fā)的demo,有些自己做了小改動(dòng)。

          PS:需要引入http相關(guān)jar包,httpcore-4.2.3.jar,httpmine-4.2.3.jar,httpclient-4.2.3.jar,httpclient-cache-4.2.3.jar。一樣,
          還是去https://repository.sonatype.org里面找吧,如果是maven建立的項(xiàng)目,就把他們引入到pom.xml里面去吧!
          posted @ 2015-09-14 12:23 休息食客 閱讀(361) | 評(píng)論 (0)編輯 收藏

          2015年9月11日 #

          第一節(jié)講了怎么準(zhǔn)備所有的參數(shù),這里講怎么將這些參數(shù)組成xml(即要通過(guò)HTTPS發(fā)送的數(shù)據(jù))
          大家可以先在網(wǎng)上了解一下java bean轉(zhuǎn)xml的相關(guān)知識(shí)
          1、我建了里一個(gè)請(qǐng)求參數(shù)類
          public class WechatCashHongbaoSendReq {
           
           private String nonce_str;             //隨機(jī)字符串
           private String sign;                  //簽名
           private String mch_billno;            //商戶訂單號(hào)
           private String mch_id;                //商戶號(hào)
           private String wxappid;               //公眾賬號(hào)appid
           private String send_name;             //商戶名稱
           private String re_openid;             //用戶openid
           private int total_amount;          //付款金額
           private int total_num;             //紅包發(fā)放總?cè)藬?shù)
           private String wishing;               //紅包祝福語(yǔ)
           private String client_ip;             //Ip地址
           private String act_name;              //活動(dòng)名稱
           private String remark;                //備注
           
           public String getNonce_str() {
            return nonce_str;
           }
           public void setNonce_str(String nonce_str) {
            this.nonce_str = nonce_str;
           }
           public String getSign() {
            return sign;
           }
           public void setSign(String sign) {
            this.sign = sign;
           }
           public String getMch_billno() {
            return mch_billno;
           }
           public void setMch_billno(String mch_billno) {
            this.mch_billno = mch_billno;
           }
           public String getMch_id() {
            return mch_id;
           }
           public void setMch_id(String mch_id) {
            this.mch_id = mch_id;
           }
           public String getWxappid() {
            return wxappid;
           }
           public void setWxappid(String wxappid) {
            this.wxappid = wxappid;
           }
           public String getSend_name() {
            return send_name;
           }
           public void setSend_name(String send_name) {
            this.send_name = send_name;
           }
           public String getRe_openid() {
            return re_openid;
           }
           public void setRe_openid(String re_openid) {
            this.re_openid = re_openid;
           }
           public int getTotal_amount() {
            return total_amount;
           }
           public void setTotal_amount(int total_amount) {
            this.total_amount = total_amount;
           }
           public int getTotal_num() {
            return total_num;
           }
           public void setTotal_num(int total_num) {
            this.total_num = total_num;
           }
           public String getWishing() {
            return wishing;
           }
           public void setWishing(String wishing) {
            this.wishing = wishing;
           }
           public String getClient_ip() {
            return client_ip;
           }
           public void setClient_ip(String client_ip) {
            this.client_ip = client_ip;
           }
           public String getAct_name() {
            return act_name;
           }
           public void setAct_name(String act_name) {
            this.act_name = act_name;
           }
           public String getRemark() {
            return remark;
           }
           public void setRemark(String remark) {
            this.remark = remark;
           }
          }

          2、然后把13個(gè)參數(shù)都set進(jìn)去
          WechatCashHongbaoSendReq sendReq = new WechatCashHongbaoSendReq();
            sendReq.setNonce_str(nonce_str);
            sendReq.setSign(sign);
            sendReq.setMch_billno(mch_billno);
            sendReq.setMch_id(mch_id);
            sendReq.setWxappid(wxappid);
            sendReq.setSend_name(send_name);
            sendReq.setRe_openid(re_openid);
            sendReq.setTotal_amount(total_amount);
            sendReq.setTotal_num(total_num);
            sendReq.setWishing(wishing);
            sendReq.setClient_ip(client_ip);
            sendReq.setAct_name(act_name);
            sendReq.setRemark(remark);
          3、將請(qǐng)求類轉(zhuǎn)為xml
          String data = Util.bean2XML(req).replaceAll("__", "_");    //為什么要加一個(gè)replaceAll("__", "_"),如果不加,會(huì)有什么情況,大家可以試試
          寫一個(gè)Util類,代碼如下:
          import com.thoughtworks.xstream.XStream;
          import com.thoughtworks.xstream.core.util.QuickWriter;
          import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
          import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
          import com.thoughtworks.xstream.io.xml.XppDriver;
          import java.io.*;
          import java.lang.reflect.Field;
          import java.util.Map;
          /**
           * User: rizenguo
           * Date: 2014/10/23
           * Time: 14:59
           */
          public class Util {
              //打log用
              /**
               * 通過(guò)反射的方式遍歷對(duì)象的屬性和屬性值,方便調(diào)試
               *
               * @param o 要遍歷的對(duì)象
               * @throws Exception
               */
              public static void reflect(Object o) throws Exception {
                  Class cls = o.getClass();
                  Field[] fields = cls.getDeclaredFields();
                  for (int i = 0; i < fields.length; i++) {
                      Field f = fields[i];
                      f.setAccessible(true);
                      Util.log(f.getName() + " -> " + f.get(o));
                  }
              }
              public static byte[] readInput(InputStream in) throws IOException {
                  ByteArrayOutputStream out = new ByteArrayOutputStream();
                  int len = 0;
                  byte[] buffer = new byte[1024];
                  while ((len = in.read(buffer)) > 0) {
                      out.write(buffer, 0, len);
                  }
                  out.close();
                  in.close();
                  return out.toByteArray();
              }
              public static String inputStreamToString(InputStream is) throws IOException {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  int i;
                  while ((i = is.read()) != -1) {
                      baos.write(i);
                  }
                  return baos.toString();
              }
              public static InputStream getStringStream(String sInputString) throws UnsupportedEncodingException {
                  ByteArrayInputStream tInputStringStream = null;
                  if (sInputString != null && !sInputString.trim().equals("")) {
                      tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("UTF-8"));
                  }
                  return tInputStringStream;
              }
              public static Object getObjectFromXML(String xml, Class tClass) {
                  //將從API返回的XML數(shù)據(jù)映射到Java對(duì)象
               xstream.alias("xml", tClass);
                  return xstream.fromXML(xml);
              }
              public static String getStringFromMap(Map<String, Object> map, String key, String defaultValue) {
                  if (key == "" || key == null) {
                      return defaultValue;
                  }
                  String result = (String) map.get(key);
                  if (result == null) {
                      return defaultValue;
                  } else {
                      return result;
                  }
              }
              public static int getIntFromMap(Map<String, Object> map, String key) {
                  if (key == "" || key == null) {
                      return 0;
                  }
                  if (map.get(key) == null) {
                      return 0;
                  }
                  return Integer.parseInt((String) map.get(key));
              }
              /**
               * 打log接口
               * @param log 要打印的log字符串
               * @return 返回log
               */
              public static String log(Object log){
                  return log.toString();
              }
              /**
               * 讀取本地的xml數(shù)據(jù),一般用來(lái)自測(cè)用
               * @param localPath 本地xml文件路徑
               * @return 讀到的xml字符串
               */
              public static String getLocalXMLString(String localPath) throws IOException {
                  return Util.inputStreamToString(Util.class.getResourceAsStream(localPath));
              }
             
              /**
               * 將java對(duì)象轉(zhuǎn)為xml字符串
               * @param clazz
               * @return
               */
              public static String bean2XML(Object o){
               xstream.alias("xml", o.getClass());
               return xstream.toXML(o);
              }
             
              /**
            * 擴(kuò)展xstream,使其支持CDATA塊
            *  
            * @date 2013-05-19
            */  
           private static XStream xstream = new XStream(new XppDriver() {  
               public HierarchicalStreamWriter createWriter(Writer out) {  
                   return new PrettyPrintWriter(out) {  
                       // 對(duì)所有xml節(jié)點(diǎn)的轉(zhuǎn)換都增加CDATA標(biāo)記  
                       boolean cdata = true;  
             
                       @SuppressWarnings("unchecked")  
                       public void startNode(String name, Class clazz) {  
                           super.startNode(name, clazz);  
                       }  
             
                       protected void writeText(QuickWriter writer, String text) {  
                           if (cdata) {  
                               writer.write("<![CDATA[");  
                               writer.write(text);  
                               writer.write("]]>");  
                           } else {  
                               writer.write(text);  
                           }  
                       }  
                   };  
               }  
           });  
             
          }
          到這一步,已經(jīng)完成了最終要發(fā)送出去的數(shù)據(jù),然后是寫http請(qǐng)求方法。
          聲明:工具類代碼參考微信支付開發(fā)的demo,有些自己做了小改動(dòng)。
          PS:需要引入xstream-1.3.1.jar包,大家可以去apache中央倉(cāng)庫(kù)找https://repository.sonatype.org,輸入xstream就可以搜索到,并下載出來(lái),如果你的項(xiàng)目是maven建立的,直接在pom.xml里面加上就行
          <dependency>
               <groupId>com.thoughtworks.xstream</groupId>
               <artifactId>xstream</artifactId>
               <version>1.3.1</version>
          </dependency>
          posted @ 2015-09-11 15:46 休息食客 閱讀(331) | 評(píng)論 (0)編輯 收藏

          2015年9月10日 #

          開發(fā)之前,最好有一些微信開發(fā)的經(jīng)驗(yàn),先看一下文檔,了解一下https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
          文檔過(guò)了一遍之后,腦海里應(yīng)該有些印象了,廢話不說(shuō),進(jìn)入開發(fā)。
          準(zhǔn)備參數(shù),一個(gè)一個(gè)來(lái)。
          參數(shù)1:隨機(jī)字符串    nonce_str
          //獲取隨機(jī)數(shù),這里設(shè)定長(zhǎng)度20,只要不成長(zhǎng)于32位
          String nonce_str = RandomStringGenerator.getRandomStringByLength(20); 

          寫一個(gè)RandomStringGenerator類,寫一個(gè)getRandomStringByLength靜態(tài)方法
          import java.util.Random;
          /**
           * User: rizenguo
           * Date: 2014/10/29
           */
          public class RandomStringGenerator {
              /**
               * 獲取一定長(zhǎng)度的隨機(jī)字符串
               * @param length 指定字符串長(zhǎng)度
               * @return 一定長(zhǎng)度的字符串
               */
              public static String getRandomStringByLength(int length) {
                  String base = "abcdefghijklmnopqrstuvwxyz0123456789";
                  Random random = new Random();
                  StringBuffer sb = new StringBuffer();
                  for (int i = 0; i < length; i++) {
                      int number = random.nextInt(base.length());
                      sb.append(base.charAt(number));
                  }
                  return sb.toString();
              }
          }
          參數(shù)2 : 商戶號(hào)   mch_id
          String mch_id = “xxxxxxxx”;

          參數(shù)3 : 商戶訂單號(hào)  mch_billno

          String mch_billno = mch_id + GenerateSequenceUtil.generateSequenceNo();

          些一個(gè)GenerateSequenceUtil類,寫一個(gè)generateSequenceNo方法

          import java.text.DecimalFormat;
          import java.text.FieldPosition;
          import java.text.Format;
          import java.text.NumberFormat;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
           
          import org.apache.log4j.Logger;
           
          public class GenerateSequenceUtil {
           
           /** .log */
           private static final Logger logger = Logger.getLogger(GenerateSequenceUtil.class);
           
           /** The FieldPosition. */
           private static final FieldPosition HELPER_POSITION = new FieldPosition(0);
           
           /** This Format for format the data to special format. */
           private final static Format dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
           
           /** This Format for format the number to special format. */
           private final static NumberFormat numberFormat = new DecimalFormat("0000");
           
           /** This int is the sequence number ,the default value is 0. */
           private static int seq = 0;
           
           private static final int MAX = 9999;
           
           /**
           * 時(shí)間格式生成序列
           * @return String
           */
           public static synchronized String generateSequenceNo() {
           
            Calendar rightNow = Calendar.getInstance();
             
            StringBuffer sb = new StringBuffer();
             
            dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
             
            numberFormat.format(seq, sb, HELPER_POSITION);
             
            if (seq == MAX) {
             seq = 0;
            } else {
             seq++;
            }
             
            logger.info("THE SQUENCE IS :" + sb.toString());
             
            return sb.toString();
           }

          參數(shù)4: 公眾賬號(hào)appid   wxappid

          這個(gè)參數(shù)可以在微信開發(fā)者中心里面看到
          //微信分配的公眾賬號(hào)ID
          String wxappid = "wx8888888888888888";
          參數(shù)5 : 商戶名稱   send_name
          隨便寫
          String send_name = "測(cè)試者";   
          參數(shù)6 : 用戶openid   re_openid
          微信號(hào)和公眾號(hào)對(duì)應(yīng)的唯一的加密過(guò)的字符串
          String re_openid = "xxxxxxxxxxxxxxxxxxxxxxx";
          參數(shù)7: 付款金額   total_amount
          int total_amount = 100;    單位分
          參數(shù)8 : 紅包發(fā)放總?cè)藬?shù)  total_num
          int total_num = 1;
          參數(shù)9 : 紅包祝福語(yǔ)   wishing
          隨便寫
          String wishing = "測(cè)試";
          參數(shù)10 : Ip地址   client_ip
          String client_ip = InetAddress.getLocalHost().getHostAddress().toString();
          這里可能報(bào)異常,需要抓取一下
          參數(shù)11 : 活動(dòng)名稱   act_name
          隨便寫
          String act_name = "測(cè)試創(chuàng)建20150906";
          參數(shù)12 : 備注  remark
          隨便寫
          String remark = "測(cè)試";
          最后一個(gè)參數(shù)sign
          //簽名
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("nonce_str", nonce_str);
            map.put("mch_billno", mch_billno);
            map.put("mch_id", mch_id);
            map.put("wxappid", wxappid);
            map.put("send_name", send_name);
            map.put("re_openid", re_openid);
            map.put("total_amount", total_amount);
            map.put("total_num", total_num);
            map.put("wishing", wishing);
            map.put("client_ip", client_ip);
            map.put("act_name", act_name);
            map.put("remark", remark);
            String sign = Signature.getSign(map);
          寫一個(gè)Signature類,代碼如下:
          import org.xml.sax.SAXException;
          import com.yxht.core.common.tools.LoadProperties;
          import javax.xml.parsers.ParserConfigurationException;
          import java.io.IOException;
          import java.lang.reflect.Field;
          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.Map;
          /**
           * User: rizenguo
           * Date: 2014/10/29
           * Time: 15:23
           */
          public class Signature {
              /**
               * 簽名算法
               * @param o 要參與簽名的數(shù)據(jù)對(duì)象
               * @return 簽名
               * @throws IllegalAccessException
               */
              public static String getSign(Object o) throws IllegalAccessException {
                  ArrayList<String> list = new ArrayList<String>();
                  Class cls = o.getClass();
                  Field[] fields = cls.getDeclaredFields();
                  for (Field f : fields) {
                      f.setAccessible(true);
                      if (f.get(o) != null && f.get(o) != "") {
                          list.add(f.getName() + "=" + f.get(o) + "&");
                      }
                  }
                  int size = list.size();
                  String [] arrayToSort = list.toArray(new String[size]);
                  Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
                  StringBuilder sb = new StringBuilder();
                  for(int i = 0; i < size; i ++) {
                      sb.append(arrayToSort[i]);
                  }
                  String result = sb.toString();
                  result += "key=" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";   //這里是開發(fā)者中心里面服務(wù)器配置里面的消息加解密密鑰
                  Util.log("Sign Before MD5:" + result);
                  result = MD5.MD5Encode(result).toUpperCase();
                  Util.log("Sign Result:" + result);
                  return result;
              }
              public static String getSign(Map<String,Object> map){
                  ArrayList<String> list = new ArrayList<String>();
                  for(Map.Entry<String,Object> entry:map.entrySet()){
                      if(entry.getValue()!=""){
                          list.add(entry.getKey() + "=" + entry.getValue() + "&");
                      }
                  }
                  int size = list.size();
                  String [] arrayToSort = list.toArray(new String[size]);
                  Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
                  StringBuilder sb = new StringBuilder();
                  for(int i = 0; i < size; i ++) {
                      sb.append(arrayToSort[i]);
                  }
                  String result = sb.toString();
                  result += "key=" + LoadProperties.getSystemdefaultValue("APP_KEY");
                  result = MD5.MD5Encode(result).toUpperCase();
                  return result;
              }
              /**
               * 從API返回的XML數(shù)據(jù)里面重新計(jì)算一次簽名
               * @param responseString API返回的XML數(shù)據(jù)
               * @return 新鮮出爐的簽名
               * @throws ParserConfigurationException
               * @throws IOException
               * @throws SAXException
               */
              public static String getSignFromResponseString(String responseString) throws IOException, SAXException, ParserConfigurationException {
                  Map<String,Object> map = XMLParser.getMapFromXML(responseString);
                  //清掉返回?cái)?shù)據(jù)對(duì)象里面的Sign數(shù)據(jù)(不能把這個(gè)數(shù)據(jù)也加進(jìn)去進(jìn)行簽名),然后用簽名算法進(jìn)行簽名
                  map.put("sign","");
                  //將API返回的數(shù)據(jù)根據(jù)用簽名算法進(jìn)行計(jì)算新的簽名,用來(lái)跟API返回的簽名進(jìn)行比較
                  return Signature.getSign(map);
              }
              /**
               * 檢驗(yàn)API返回的數(shù)據(jù)里面的簽名是否合法,避免數(shù)據(jù)在傳輸?shù)倪^(guò)程中被第三方篡改
               * @param responseString API返回的XML數(shù)據(jù)字符串
               * @return API簽名是否合法
               * @throws ParserConfigurationException
               * @throws IOException
               * @throws SAXException
               */
              public static boolean checkIsSignValidFromResponseString(String responseString) throws ParserConfigurationException, IOException, SAXException {
                  Map<String,Object> map = XMLParser.getMapFromXML(responseString);
                  Util.log(map.toString());
                  String signFromAPIResponse = map.get("sign").toString();
                  if(signFromAPIResponse=="" || signFromAPIResponse == null){
                      Util.log("API返回的數(shù)據(jù)簽名數(shù)據(jù)不存在,有可能被第三方篡改!!!");
                      return false;
                  }
                  Util.log("服務(wù)器回包里面的簽名是:" + signFromAPIResponse);
                  //清掉返回?cái)?shù)據(jù)對(duì)象里面的Sign數(shù)據(jù)(不能把這個(gè)數(shù)據(jù)也加進(jìn)去進(jìn)行簽名),然后用簽名算法進(jìn)行簽名
                  map.put("sign","");
                  //將API返回的數(shù)據(jù)根據(jù)用簽名算法進(jìn)行計(jì)算新的簽名,用來(lái)跟API返回的簽名進(jìn)行比較
                  String signForAPIResponse = Signature.getSign(map);
                  if(!signForAPIResponse.equals(signFromAPIResponse)){
                      //簽名驗(yàn)不過(guò),表示這個(gè)API返回的數(shù)據(jù)有可能已經(jīng)被篡改了
                      Util.log("API返回的數(shù)據(jù)簽名驗(yàn)證不通過(guò),有可能被第三方篡改!!!");
                      return false;
                  }
                  Util.log("恭喜,API返回的數(shù)據(jù)簽名驗(yàn)證通過(guò)!!!");
                  return true;
              }
          }
          先講到這里,所有參數(shù)都準(zhǔn)備好了,下一步是組成xml
          聲明:工具類代碼參考微信支付開發(fā)的demo,有些自己做了小改動(dòng)。
          posted @ 2015-09-10 15:35 休息食客 閱讀(881) | 評(píng)論 (0)編輯 收藏

          2015年2月11日 #

               摘要: java解析xml文件的方式有幾種,下面介紹用dom4j方式解析1、先看一段xml內(nèi)容 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->  1 <configs>  2 &nb...  閱讀全文
          posted @ 2015-02-11 17:23 休息食客 閱讀(423) | 評(píng)論 (0)編輯 收藏

          linux發(fā)行版:CentOS release 6.5 (Final)
          啟動(dòng)系統(tǒng)后,沒(méi)有桌面,只是一個(gè)控制臺(tái),如果要啟動(dòng)桌面系統(tǒng),如下步驟:
          1、啟動(dòng)系統(tǒng),登錄
          2、vi /etc/inittab
          3、將最后一行的id:3:initdefault:中的3換成5,即id:5:initdefault:.
          4、wq保存退出
          5、重啟系統(tǒng)即可。

          說(shuō)明:如果將桌面系統(tǒng)切換成控制臺(tái),反過(guò)來(lái)將5換成3重啟即可。 此方法只在上面說(shuō)的發(fā)行版測(cè)試通過(guò)。
          posted @ 2015-02-11 11:01 休息食客 閱讀(363) | 評(píng)論 (1)編輯 收藏

          2015年2月5日 #

          不允許將 NULL 值賦給 NOT NULL 列"TBSPACEID=3, TABLEID=34, COLNO=29"。

          SELECT C.TABSCHEMA, C.TABNAME, C.COLNAME   FROM SYSCAT.TABLES AS T,SYSCAT.COLUMNS AS C WHERE T.TBSPACEID = 3 AND T.TABLEID = 34 AND C.COLNO = 29 AND C.TABSCHEMA = T.TABSCHEMA AND C.TABNAME = T.TABNAME

          找出是哪一列不能插入空值。
          posted @ 2015-02-05 15:50 休息食客 閱讀(398) | 評(píng)論 (0)編輯 收藏

          2015年1月30日 #

          Java獲取根目錄路徑有很多種方法,但是根據(jù)應(yīng)用程序所部署的環(huán)境和中間件不同,獲取的路徑可能也不同。下面我提供一種方法,解決這個(gè)問(wèn)題。

                   String path = InterfaceConfig.class.getResource("").getPath().toString();//獲取類所在路徑
                   if (path.contains(".jar")) {
                       path = path.replace("/", File.separator);//將/換成\,如果是linux環(huán)境,還是/
                       path = path.replace("file:", "");//去掉file
                       path = path.replace("classes\\", "");//去掉classes\
                       if (path.startsWith("\\")) {
                           path = path.substring(1);//去掉第一個(gè)\,如:、\D:\TongWeb...  ,在linux上沒(méi)有這種情況
                       }
                       path = path.split("WEB-INF")[0]+"WEB-INF"+File.separator+"classes";
                  } else {
                       path = InterfaceConfig.class.getResource("/").getPath().toString();//獲取根路徑
                  }
                  File file = new File(path + File.separator + "InterfaceConfig.xml");

          這段代碼什么意思呢?
          1、第一行是獲取InterfaceConfig.class這個(gè)類所在的路徑,這個(gè)InterfaceConfig.class可以換成你這段代碼所在的類。
          2、判斷路徑中是否包含.jar,就是說(shuō)這段代碼所在的類最終構(gòu)建的時(shí)候是不是打入jar包里,如果在jar包里,就去掉一些內(nèi)容,如file: ,classes\\,
              然后根據(jù)WEB-INF切割,并拼出根目錄。
          3、如果沒(méi)有打入jar包,那更簡(jiǎn)單了,直接通過(guò)獲取根路徑方法就解決了。
          4、最后讀取classes路徑下的InterfaceConfig.xml配置文件。

          posted @ 2015-01-30 15:01 休息食客 閱讀(1332) | 評(píng)論 (1)編輯 收藏

          2014年12月25日 #

          create or replace trigger preventdeleted502
          before delete on d502_mzdetail
          for each row
          begin
          if deleting then raise_application_error(-200001,'不允許刪除數(shù)據(jù)!');
          dbms_output.PUT_LINE('不允許刪除數(shù)據(jù)!');
          end if;
          end;

          解釋說(shuō)明:preventdeleted502為該觸發(fā)器名稱,d502_mzdetail為表名,對(duì)于表d502_mzdetail如果一有刪除動(dòng)作,馬上提示溢出錯(cuò)誤,同時(shí)打印錯(cuò)誤信息,那么刪除動(dòng)作即刻停止!
          posted @ 2014-12-25 10:15 休息食客 閱讀(3501) | 評(píng)論 (0)編輯 收藏

          2014年11月12日 #

          我用的是CentOS6.5 32位
          1、安裝ftp
          sudo yum install vsftpd   (注:如果你用的是Ubuntu,命令:sudo apt-get install vsftpd)

          2、配置vsftpd.conf
          sudo vim /etc/vsftpd.conf (注:這里的vim是編輯器,如果沒(méi)有安裝,請(qǐng)先sudo yum install vim安裝這個(gè)編輯器,當(dāng)然也可以用系統(tǒng)自動(dòng)的vi)

          #禁止匿名訪問(wèn) 
          anonymous_enable=NO
          #接受本地用戶
          local_enable=YES
          #允許上傳
          write_enable=YES
          #用戶只能訪問(wèn)限制的目錄
          chroot_local_user=YES
          #設(shè)置固定目錄,在結(jié)尾添加。如果不添加這一行,各用戶對(duì)應(yīng)自己的目錄,當(dāng)然這個(gè)文件夾自己建
          local_root=/home/ftp

          3、添加用戶
          sudo useradd -d /home/ftp -M loub 
          sudo passwd loub

          4、給文件夾授權(quán)
          sudo chmod a-w /home/ftp 
          sudo mkdir /home/ftp/upload
          sudo mkdir /home/ftp/download
          sudo chmod -R 777 /home/ftp/upload
          sudo chmod -R 777 /home/ftp/download

          5、
          命令:
          sudo vim /etc/pam.d/vsftpd
          注釋掉
          #auth    required pam_shells.so

          6、重啟服務(wù)
          sudo service vsftpd restart

          安裝ftp服務(wù)器到這里就差不多了,然后在客戶機(jī)上安裝一個(gè)FileZilla軟件,輸入服務(wù)器ip,用戶名和密碼,端口默認(rèn)21就行了。
          如果想了解vsftpd更多信息網(wǎng)上有很多,但我覺得有這幾個(gè)步驟就差不多了,能用就行,沒(méi)必要搞清楚原理。

          我用ftp遇到的問(wèn)題,每次當(dāng)我啟動(dòng)了CentOS后,然后用FileZilla連上服務(wù)器,就拒絕訪問(wèn),因?yàn)镃entOS上的ftp沒(méi)有啟動(dòng),于是輸入
          sudo /etc/init.d/vsftpd start 就會(huì)報(bào)錯(cuò):500 oops :connot read config file....
          這是因?yàn)闆](méi)有激活vsftpd,還要設(shè)置運(yùn)行級(jí)別,那么就用root用戶登錄
          命令:su root
          命令:chkconfig --level 35 vsftpd on
          命令:service vsftpd restart
          就可以用FileZilla連上了。
          posted @ 2014-11-12 10:00 休息食客 閱讀(700) | 評(píng)論 (0)編輯 收藏

          僅列出標(biāo)題  下一頁(yè)
          主站蜘蛛池模板: 阿合奇县| 台中市| 仙桃市| 郎溪县| 达日县| 雷山县| 高安市| 喀什市| 五指山市| 凉山| 望奎县| 开化县| 阜阳市| 荣成市| 响水县| 陕西省| 张掖市| 苗栗市| 大姚县| 信阳市| 镇平县| 武义县| 通河县| 永仁县| 筠连县| 年辖:市辖区| 广西| 浠水县| 石家庄市| 志丹县| 杭锦旗| 洛隆县| 抚松县| 敦煌市| 邯郸县| 蛟河市| 郧西县| 西华县| 吉隆县| 河南省| 安多县|