微信支付-转载不写明出处 小程序实现微信支付java后端

小程序实现微信支付java后端

第一步 

进入小程序,下单,请求下单支付,调用小程序登录API来获取Openid(https://mp.weixin.qq.com/debug/w ... .html#wxloginobject),生成商户订单,这些都是在小程序端完成的业务。 

小程序端代码 

[javascript]  view plain  copy
  1. // pages/pay/pay.js   
  2. var app = getApp();   
  3. Page({   
  4.     data: {},   
  5.     onLoad: function (options) {   
  6.         // 页面初始化 options为页面跳转所带来的参数   
  7.     },   
  8.     /* 微信支付 */   
  9.     wxpay: function () {   
  10.         var that = this   
  11.         //登陆获取code   
  12.         wx.login({   
  13.             success: function (res) {   
  14.                 console.log(res.code)   
  15.                 //获取openid   
  16.                 that.getOpenId(res.code)   
  17.             }   
  18.         });   
  19.     },   
  20.     getOpenId: function (code) {   
  21.         var that = this;   
  22.         wx.request({   
  23.             url: "https://api.weixin.qq.com/sns/jscode2session?appid=wxa142513e524e496c&secret=5d6a7d86048884e7c60f84f7aa85253c&js_code=" + code + "&grant_type=authorization_code",   
  24.             data: {},   
  25.             method: 'GET',   
  26.             success: function (res) {   
  27.                 console.log('返回openId')   
  28.                 console.log(res.data)   
  29.                 that.generateOrder(res.data.openid)   
  30.             },   
  31.             fail: function () {   
  32.                 // fail   
  33.             },   
  34.             complete: function () {   
  35.                 // complete   
  36.             }   
  37.         })   
  38.     },   
  39.     /**生成商户订单 */   
  40.     generateOrder: function (openid) {   
  41.         var that = this   
  42.         //统一支付   
  43.         wx.request({   
  44.             url: 'http://localhost:8070/RMS/pay_pay.action',   
  45.             method: 'GET',   
  46.             data: {   
  47.                 total_fee: '5',   
  48.                 body: '支付测试',   
  49.                 attach:'真假酒水'   
  50.             },   
  51.             success: function (res) {   
  52.                 console.log(res)   
  53.                 var pay = res.data   
  54.                 //发起支付   
  55.                 var timeStamp = pay[0].timeStamp;   
  56.                 console.log("timeStamp:"+timeStamp)   
  57.                 var packages = pay[0].package;   
  58.                 console.log("package:"+packages)   
  59.                 var paySign = pay[0].paySign;   
  60.                 console.log("paySign:"+paySign)   
  61.                 var nonceStr = pay[0].nonceStr;   
  62.                  console.log("nonceStr:"+nonceStr)   
  63.                 var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType""MD5""nonceStr": nonceStr };   
  64.                 that.pay(param)   
  65.             },   
  66.         })   
  67.     },   
  68.     /* 支付   */   
  69.     pay: function (param) {   
  70.         console.log("支付")   
  71.         console.log(param)   
  72.         wx.requestPayment({   
  73.             timeStamp: param.timeStamp,   
  74.             nonceStr: param.nonceStr,   
  75.             package: param.package,   
  76.             signType: param.signType,   
  77.             paySign: param.paySign,   
  78.             success: function (res) {   
  79.                 // success   
  80.                 console.log("支付")   
  81.                 console.log(res)   
  82.                 wx.navigateBack({   
  83.                     delta: 1, // 回退前 delta(默认为1) 页面   
  84.                     success: function (res) {   
  85.                         wx.showToast({   
  86.                             title: '支付成功',   
  87.                             icon: 'success',   
  88.                             duration: 2000   
  89.                         })   
  90.                     },   
  91.                     fail: function () {   
  92.                         // fail   
  93.                     },   
  94.                     complete: function () {   
  95.                         // complete   
  96.                     }   
  97.                 })   
  98.             },   
  99.             fail: function (res) {   
  100.                 // fail   
  101.                 console.log("支付失败")   
  102.                 console.log(res)   
  103.             },   
  104.             complete: function () {   
  105.                 // complete   
  106.                 console.log("pay complete")   
  107.             }   
  108.         })   
  109.     }   
  110. })   
第二步 

调用支付统一下单API来获取prepay_id,并将小程序调起支付数据需要签名的字段appId,timeStamp,nonceStr,package再次签名(https://pay.weixin.qq.com/wiki/d ... ter=7_7&index=3) 

后台代码 
[html]  view plain  copy
  1. package cn.it.shop.action;   
  2. import java.io.ByteArrayInputStream;   
  3. import java.io.InputStream;   
  4. import java.io.UnsupportedEncodingException;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.Date;   
  7. import java.util.HashMap;   
  8. import java.util.List;   
  9. import java.util.Map;   
  10. import org.dom4j.Document;   
  11. import org.dom4j.DocumentException;   
  12. import org.dom4j.Element;   
  13. import org.dom4j.io.SAXReader;   
  14. import cn.it.shop.util.MessageUtil;   
  15. import cn.it.shop.util.PayUtil;   
  16. import cn.it.shop.util.PaymentPo;   
  17. import cn.it.shop.util.UUIDHexGenerator;   
  18. import net.sf.json.JSONArray;   
  19. import net.sf.json.JSONObject;   
  20. /**   
  21. * @author   
  22. * @version 创建时间:2017年1月21日 下午4:59:03   
  23. * 小程序端请求的后台action,返回签名后的数据传到前台   
  24. */   
  25. public class PayAction {   
  26.     private String total_fee;//总金额   
  27.     private String body;//商品描述   
  28.     private String detail;//商品详情      
  29.     private String attach;//附加数据   
  30.     private String time_start;//交易起始时间   
  31.     private String time_expire;//交易结束时间   
  32.     private String openid;//用户标识   
  33.       
  34.     public String pay() throws UnsupportedEncodingException, DocumentException{   
  35. private JSONArray jsonArray=new JSONArray();   
  36.          body = new String(body.getBytes("UTF-8"),"ISO-8859-1");    
  37.         String appid = "替换为自己的小程序ID";//小程序ID   
  38.         String mch_id = "替换为自己的商户号";//商户号   
  39.         String nonce_str = UUIDHexGenerator.generate();//随机字符串   
  40.         String today = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());   
  41.         String code = PayUtil.createCode(8);   
  42.         String out_trade_no = mch_id+today+code;//商户订单号   
  43.         String spbill_create_ip = "替换为自己的终端IP";//终端IP   
  44.         String notify_url = "http://www.weixin.qq.com/wxpay/pay.php";//通知地址   
  45.         String trade_type = "JSAPI";//交易类型    
  46.         String openid="替换为用户的openid";//用户标识   
  47.         /**/   
  48.         PaymentPo paymentPo = new PaymentPo();   
  49.         paymentPo.setAppid(appid);   
  50.         paymentPo.setMch_id(mch_id);   
  51.         paymentPo.setNonce_str(nonce_str);   
  52.         String newbody=new String(body.getBytes("ISO-8859-1"),"UTF-8");//以utf-8编码放入paymentPo,微信支付要求字符编码统一采用UTF-8字符编码   
  53.         paymentPo.setBody(newbody);   
  54.         paymentPo.setOut_trade_no(out_trade_no);   
  55.         paymentPo.setTotal_fee(total_fee);   
  56.         paymentPo.setSpbill_create_ip(spbill_create_ip);   
  57.         paymentPo.setNotify_url(notify_url);   
  58.         paymentPo.setTrade_type(trade_type);   
  59.         paymentPo.setOpenid(openid);   
  60.         // 把请求参数打包成数组   
  61.         Map sParaTemp = new HashMap();   
  62.         sParaTemp.put("appid", paymentPo.getAppid());   
  63.         sParaTemp.put("mch_id", paymentPo.getMch_id());   
  64.         sParaTemp.put("nonce_str", paymentPo.getNonce_str());   
  65.         sParaTemp.put("body",  paymentPo.getBody());   
  66.         sParaTemp.put("out_trade_no", paymentPo.getOut_trade_no());   
  67.         sParaTemp.put("total_fee",paymentPo.getTotal_fee());   
  68.         sParaTemp.put("spbill_create_ip", paymentPo.getSpbill_create_ip());   
  69.         sParaTemp.put("notify_url",paymentPo.getNotify_url());   
  70.         sParaTemp.put("trade_type", paymentPo.getTrade_type());   
  71.         sParaTemp.put("openid", paymentPo.getOpenid());   
  72.         // 除去数组中的空值和签名参数   
  73.         Map sPara = PayUtil.paraFilter(sParaTemp);   
  74.         String prestr = PayUtil.createLinkString(sPara); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串   
  75.         String key = "&key=替换为商户支付密钥"; // 商户支付密钥   
  76.         //MD5运算生成签名   
  77.         String mysign = PayUtil.sign(prestr, key, "utf-8").toUpperCase();   
  78.         paymentPo.setSign(mysign);   
  79.         //打包要发送的xml   
  80.         String respXml = MessageUtil.messageToXML(paymentPo);   
  81.         // 打印respXml发现,得到的xml中有“__”不对,应该替换成“_”   
  82.         respXml = respXml.replace("__", "_");   
  83.         String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//统一下单API接口链接   
  84.         String param = respXml;   
  85.         //String result = SendRequestForUrl.sendRequest(url, param);//发起请求   
  86.         String result =PayUtil.httpRequest(url, "POST", param);   
  87.         // 将解析结果存储在HashMap中   
  88.         Map map = new HashMap();   
  89.          InputStream in=new ByteArrayInputStream(result.getBytes());    
  90.         // 读取输入流   
  91.         SAXReader reader = new SAXReader();   
  92.         Document document = reader.read(in);   
  93.         // 得到xml根元素   
  94.         Element root = document.getRootElement();   
  95.         // 得到根元素的所有子节点   
  96.         @SuppressWarnings("unchecked")   
  97.         List elementList = root.elements();   
  98.         for (Element element : elementList) {   
  99.             map.put(element.getName(), element.getText());   
  100.         }   
  101.         // 返回信息   
  102.         String return_code = map.get("return_code");//返回状态码   
  103.         String return_msg = map.get("return_msg");//返回信息   
  104.         System.out.println("return_msg"+return_msg);   
  105.         JSONObject JsonObject=new JSONObject() ;   
  106.         if(return_code=="SUCCESS"||return_code.equals(return_code)){   
  107.             // 业务结果   
  108.             String prepay_id = map.get("prepay_id");//返回的预付单信息   
  109.             String nonceStr=UUIDHexGenerator.generate();   
  110.             JsonObject.put("nonceStr", nonceStr);   
  111.             JsonObject.put("package", "prepay_id="+prepay_id);   
  112.             Long timeStampSystem.currentTimeMillis()/1000;   
  113.             JsonObject.put("timeStamp", timeStamp+"");   
  114.             String stringSignTemp = "appId="+appid+"&nonceStr=" + nonceStr + "&package=prepay_id=" + prepay_id+ "&signType=MD5&timeStamp=" + timeStamp;   
  115.             //再次签名   
  116.             String paySign=PayUtil.sign(stringSignTemp, "&key=替换为自己的密钥", "utf-8").toUpperCase();   
  117.             JsonObject.put("paySign", paySign);   
  118.             jsonArray.add(JsonObject);   
  119.         }   
  120.         return "pay";   
  121.     }   
  122.     public String getTotal_fee() {   
  123.         return total_fee;   
  124.     }   
  125.     public void setTotal_fee(String total_fee) {   
  126.         this.total_fee = total_fee;   
  127.     }   
  128.     public String getBody() {   
  129.         return body;   
  130.     }   
  131.     public void setBody(String body) {   
  132.         this.body = body;   
  133.     }   
  134.     public JSONArray getJsonArray() {   
  135.         return jsonArray;   
  136.     }   
  137.     public void setJsonArray(JSONArray jsonArray) {   
  138.         this.jsonArray = jsonArray;   
  139.     }   
  140.     public String getDetail() {   
  141.         return detail;   
  142.     }   
  143.     public void setDetail(String detail) {   
  144.         this.detail = detail;   
  145.     }   
  146.     public String getAttach() {   
  147.         return attach;   
  148.     }   
  149.     public void setAttach(String attach) {   
  150.         this.attach = attach;   
  151.     }   
  152.     public String getTime_start() {   
  153.         return time_start;   
  154.     }   
  155.     public void setTime_start(String time_start) {   
  156.         this.time_start = time_start;   
  157.     }   
  158.     public String getTime_expire() {   
  159.         return time_expire;   
  160.     }   
  161.     public void setTime_expire(String time_expire) {   
  162.         this.time_expire = time_expire;   
  163.     }   
  164.     public String getOpenid() {   
  165.         return openid;   
  166.     }   
  167.     public void setOpenid(String openid) {   
  168.         this.openid = openid;   
  169.     }   
  170. }   
后台业务逻辑涉及到的工具类及参数封装类 
MessageUtil 
[java]  view plain  copy
  1. package cn.it.shop.util;   
  2. import java.io.IOException;   
  3. import java.io.Writer;   
  4. import java.util.HashMap;   
  5. import java.util.List;   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import org.dom4j.Document;   
  8. import org.dom4j.Element;   
  9. import org.dom4j.io.SAXReader;   
  10. import com.thoughtworks.xstream.XStream;   
  11. import com.thoughtworks.xstream.core.util.QuickWriter;   
  12. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;   
  13. import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;   
  14. import com.thoughtworks.xstream.io.xml.XppDriver;   
  15. public class MessageUtil {   
  16.     public static HashMap parseXML(HttpServletRequest request) throws Exception, IOException{   
  17.         HashMap map=new HashMap();   
  18.         // 通过IO获得Document   
  19.         SAXReader reader = new SAXReader();   
  20.         Document doc = reader.read(request.getInputStream());   
  21.         //得到xml的根节点   
  22.         Element root=doc.getRootElement();   
  23.         recursiveParseXML(root,map);   
  24.         return map;   
  25.     }   
  26.     private static void recursiveParseXML(Element root,HashMap map){   
  27.         //得到根节点的子节点列表   
  28.         List elementList=root.elements();   
  29.         //判断有没有子元素列表   
  30.         if(elementList.size()==0){   
  31.             map.put(root.getName(), root.getTextTrim());   
  32.         }   
  33.         else{   
  34.             //遍历   
  35.             for(Element e:elementList){   
  36.                 recursiveParseXML(e,map);   
  37.             }   
  38.         }   
  39.     }   
  40.     private static XStream xstream = new XStream(new XppDriver() {   
  41.         public HierarchicalStreamWriter createWriter(Writer out) {   
  42.             return new PrettyPrintWriter(out) {   
  43.                 // 对所有xml节点都增加CDATA标记   
  44.                 boolean cdata = true;   
  45.                 public void startNode(String name, Class clazz) {   
  46.                     super.startNode(name, clazz);   
  47.                 }   
  48.                 protected void writeText(QuickWriter writer, String text) {   
  49.                     if (cdata) {   
  50.                         writer.write("                        writer.write(text);   
  51.                         writer.write("]]>");   
  52.                     } else {   
  53.                         writer.write(text);   
  54.                     }   
  55.                 }   
  56.             };   
  57.         }   
  58.     });   
  59.     public static String messageToXML(PaymentPo paymentPo){   
  60.         xstream.alias("xml",PaymentPo.class);   
  61.         return xstream.toXML(paymentPo);   
  62.     }   
  63. }   
PaymentPo//封装支付参数实体 
[java]  view plain  copy
  1. package cn.it.shop.util;   
  2. /**  
  3. * @author  
  4. * @version 创建时间:2017年1月21日 下午4:20:52  
  5. * 类说明  
  6. */   
  7. public class PaymentPo {   
  8.     private String appid;//小程序ID   
  9.     private String mch_id;//商户号   
  10.     private String device_info;//设备号   
  11.     private String nonce_str;//随机字符串   
  12.     private String sign;//签名   
  13.     private String body;//商品描述    
  14.     private String detail;//商品详情      
  15.     private String attach;//附加数据   
  16.     private String out_trade_no;//商户订单号   
  17.     private String fee_type;//货币类型   
  18.     private String spbill_create_ip;//终端IP   
  19.     private String time_start;//交易起始时间   
  20.     private String time_expire;//交易结束时间   
  21.     private String goods_tag;//商品标记   
  22.     private String total_fee;//总金额   
  23.     private String notify_url;//通知地址      
  24.     private String trade_type;//交易类型      
  25.     private String limit_pay;//指定支付方式   
  26.     private String openid;//用户标识   
  27.     public String getAppid() {   
  28.         return appid;   
  29.     }   
  30.     public void setAppid(String appid) {   
  31.         this.appid = appid;   
  32.     }   
  33.     public String getMch_id() {   
  34.         return mch_id;   
  35.     }   
  36.     public void setMch_id(String mch_id) {   
  37.         this.mch_id = mch_id;   
  38.     }   
  39.     public String getNonce_str() {   
  40.         return nonce_str;   
  41.     }   
  42.     public void setNonce_str(String nonce_str) {   
  43.         this.nonce_str = nonce_str;   
  44.     }   
  45.     public String getSign() {   
  46.         return sign;   
  47.     }   
  48.     public void setSign(String sign) {   
  49.         this.sign = sign;   
  50.     }   
  51.     public String getBody() {   
  52.         return body;   
  53.     }   
  54.     public void setBody(String body) {   
  55.         this.body = body;   
  56.     }   
  57.     public String getOut_trade_no() {   
  58.         return out_trade_no;   
  59.     }   
  60.     public void setOut_trade_no(String out_trade_no) {   
  61.         this.out_trade_no = out_trade_no;   
  62.     }   
  63.     public String getTotal_fee() {   
  64.         return total_fee;   
  65.     }   
  66.     public void setTotal_fee(String total_fee) {   
  67.         this.total_fee = total_fee;   
  68.     }   
  69.     public String getNotify_url() {   
  70.         return notify_url;   
  71.     }   
  72.     public void setNotify_url(String notify_url) {   
  73.         this.notify_url = notify_url;   
  74.     }   
  75.     public String getTrade_type() {   
  76.         return trade_type;   
  77.     }   
  78.     public void setTrade_type(String trade_type) {   
  79.         this.trade_type = trade_type;   
  80.     }   
  81.     public String getOpenid() {   
  82.         return openid;   
  83.     }   
  84.     public void setOpenid(String openid) {   
  85.         this.openid = openid;   
  86.     }   
  87.     public String getSpbill_create_ip() {   
  88.         return spbill_create_ip;   
  89.     }   
  90.     public void setSpbill_create_ip(String spbill_create_ip) {   
  91.         this.spbill_create_ip = spbill_create_ip;   
  92.     }   
  93.     public String getDevice_info() {   
  94.         return device_info;   
  95.     }   
  96.     public void setDevice_info(String device_info) {   
  97.         this.device_info = device_info;   
  98.     }   
  99.     public String getDetail() {   
  100.         return detail;   
  101.     }   
  102.     public void setDetail(String detail) {   
  103.         this.detail = detail;   
  104.     }   
  105.     public String getAttach() {   
  106.         return attach;   
  107.     }   
  108.     public void setAttach(String attach) {   
  109.         this.attach = attach;   
  110.     }   
  111.     public String getFee_type() {   
  112.         return fee_type;   
  113.     }   
  114.     public void setFee_type(String fee_type) {   
  115.         this.fee_type = fee_type;   
  116.     }   
  117.     public String getTime_start() {   
  118.         return time_start;   
  119.     }   
  120.     public void setTime_start(String time_start) {   
  121.         this.time_start = time_start;   
  122.     }   
  123.     public String getTime_expire() {   
  124.         return time_expire;   
  125.     }   
  126.     public void setTime_expire(String time_expire) {   
  127.         this.time_expire = time_expire;   
  128.     }   
  129.     public String getGoods_tag() {   
  130.         return goods_tag;   
  131.     }   
  132.     public void setGoods_tag(String goods_tag) {   
  133.         this.goods_tag = goods_tag;   
  134.     }   
  135.     public String getLimit_pay() {   
  136.         return limit_pay;   
  137.     }   
  138.     public void setLimit_pay(String limit_pay) {   
  139.         this.limit_pay = limit_pay;   
  140.     }   
  141. }   
PayUtil 
[java]  view plain  copy
  1. package cn.it.shop.util;   
  2. import java.io.BufferedReader;   
  3. import java.io.InputStream;   
  4. import java.io.InputStreamReader;   
  5. import java.io.OutputStream;   
  6. import java.io.UnsupportedEncodingException;   
  7. import java.net.HttpURLConnection;   
  8. import java.net.URL;   
  9. import java.util.ArrayList;   
  10. import java.util.Collections;   
  11. import java.util.HashMap;   
  12. import java.util.List;   
  13. import java.util.Map;   
  14. import org.apache.commons.codec.digest.DigestUtils;   
  15. /**  
  16. * @author  
  17. * @version 创建时间:2017年1月17日 下午7:46:29 类说明  
  18. */   
  19. public class PayUtil {   
  20.     /**  
  21.      * 签名字符串  
  22.      * @param text需要签名的字符串  
  23.      * @param key 密钥  
  24.      * @param input_charset编码格式  
  25.      * @return 签名结果  
  26.      */   
  27.     public static String sign(String text, String key, String input_charset) {   
  28.         text = text + key;   
  29.         return DigestUtils.md5Hex(getContentBytes(text, input_charset));   
  30.     }   
  31.     /**  
  32.      * 签名字符串  
  33.      *  @param text需要签名的字符串  
  34.      * @param sign 签名结果  
  35.      * @param key密钥  
  36.      * @param input_charset 编码格式  
  37.      * @return 签名结果  
  38.      */   
  39.     public static boolean verify(String text, String sign, String key, String input_charset) {   
  40.         text = text + key;   
  41.         String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));   
  42.         if (mysign.equals(sign)) {   
  43.             return true;   
  44.         } else {   
  45.             return false;   
  46.         }   
  47.     }   
  48.     /**  
  49.      * @param content  
  50.      * @param charset  
  51.      * @return  
  52.      * @throws SignatureException  
  53.      * @throws UnsupportedEncodingException  
  54.      */   
  55.     public static byte[] getContentBytes(String content, String charset) {   
  56.         if (charset == null || "".equals(charset)) {   
  57.             return content.getBytes();   
  58.         }   
  59.         try {   
  60.             return content.getBytes(charset);   
  61.         } catch (UnsupportedEncodingException e) {   
  62.             throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);   
  63.         }   
  64.     }   
  65.     /**  
  66.      * 生成6位或10位随机数 param codeLength(多少位)  
  67.      * @return  
  68.      */   
  69.     public static String createCode(int codeLength) {   
  70.         String code = "";   
  71.         for (int i = 0; i < codeLength; i++) {   
  72.             code += (int) (Math.random() * 9);   
  73.         }   
  74.         return code;   
  75.     }   
  76.     private static boolean isValidChar(char ch) {   
  77.         if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))   
  78.             return true;   
  79.         if ((ch >= 0x4e00 && ch <= 0x7fff) || (ch >= 0x8000 && ch <= 0x952f))   
  80.             return true;// 简体中文汉字编码   
  81.         return false;   
  82.     }   
  83.     /**  
  84.      * 除去数组中的空值和签名参数  
  85.      * @param sArray 签名参数组  
  86.      * @return 去掉空值与签名参数后的新签名参数组  
  87.      */   
  88.     public static Map paraFilter(Map sArray) {   
  89.         Map result = new HashMap();   
  90.         if (sArray == null || sArray.size() <= 0) {   
  91.             return result;   
  92.         }   
  93.         for (String key : sArray.keySet()) {   
  94.             String value = sArray.get(key);   
  95.             if (value == null || value.equals("") || key.equalsIgnoreCase("sign")   
  96.                     || key.equalsIgnoreCase("sign_type")) {   
  97.                 continue;   
  98.             }   
  99.             result.put(key, value);   
  100.         }   
  101.         return result;   
  102.     }   
  103.     /**  
  104.      * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串  
  105.      * @param params 需要排序并参与字符拼接的参数组  
  106.      * @return 拼接后字符串  
  107.      */   
  108.     public static String createLinkString(Map params) {   
  109.         List keys = new ArrayList(params.keySet());   
  110.         Collections.sort(keys);   
  111.         String prestr = "";   
  112.         for (int i = 0; i < keys.size(); i++) {   
  113.             String key = keys.get(i);   
  114.             String value = params.get(key);   
  115.             if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符   
  116.                 prestr = prestr + key + "=" + value;   
  117.             } else {   
  118.                 prestr = prestr + key + "=" + value + "&";   
  119.             }   
  120.         }   
  121.         return prestr;   
  122.     }   
  123.     /**  
  124.      *  
  125.      * @param requestUrl请求地址  
  126.      * @param requestMethod请求方法  
  127.      * @param outputStr参数  
  128.      */   
  129.     public static String httpRequest(String requestUrl,String requestMethod,String outputStr){   
  130.         // 创建SSLContext   
  131.         StringBuffer buffer=null;   
  132.         try{   
  133.         URL url = new URL(requestUrl);   
  134.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
  135.         conn.setRequestMethod(requestMethod);   
  136.         conn.setDoOutput(true);   
  137.         conn.setDoInput(true);   
  138.         conn.connect();   
  139.         //往服务器端写内容   
  140.         if(null !=outputStr){   
  141.             OutputStream os=conn.getOutputStream();   
  142.             os.write(outputStr.getBytes("utf-8"));   
  143.             os.close();   
  144.         }   
  145.         // 读取服务器端返回的内容   
  146.         InputStream is = conn.getInputStream();   
  147.         InputStreamReader isr = new InputStreamReader(is, "utf-8");   
  148.         BufferedReader br = new BufferedReader(isr);   
  149.         buffer = new StringBuffer();   
  150.         String line = null;   
  151.         while ((line = br.readLine()) != null) {   
  152.                       buffer.append(line);   
  153.         }   
  154.         }catch(Exception e){   
  155.             e.printStackTrace();   
  156.         }   
  157.         return buffer.toString();   
  158.         }     
  159.     public static String urlEncodeUTF8(String source){   
  160.         String result=source;   
  161.         try {   
  162.             result=java.net.URLEncoder.encode(source, "UTF-8");   
  163.         } catch (UnsupportedEncodingException e) {   
  164.             // TODO Auto-generated catch block   
  165.             e.printStackTrace();   
  166.         }   
  167.         return result;   
  168.     }   
  169. }   
UUIDHexGenerator 
[java]  view plain  copy
  1. package cn.it.shop.util;   
  2. import java.net.InetAddress;   
  3. /**  
  4. * @author  
  5. * @version 创建时间:2017年1月17日 下午7:45:06 类说明  
  6. */   
  7. public class UUIDHexGenerator {   
  8.     private static String sep = "";   
  9.     private static final int IP;   
  10.     private static short counter = (short0;   
  11.     private static final int JVM = (int) (System.currentTimeMillis() >>>;   
  12.     private static UUIDHexGenerator uuidgen = new UUIDHexGenerator();   
  13.     static {   
  14.         int ipadd;   
  15.         try {   
  16.             ipadd = toInt(InetAddress.getLocalHost().getAddress());   
  17.         } catch (Exception e) {   
  18.             ipadd = 0;   
  19.         }   
  20.         IP = ipadd;   
  21.     }   
  22.     public static UUIDHexGenerator getInstance() {   
  23.         return uuidgen;   
  24.     }   
  25.     public static int toInt(byte[] bytes) {   
  26.         int result = 0;   
  27.         for (int i = 0; i < 4; i++) {   
  28.             result = (result << - Byte.MIN_VALUE + (int) bytes;   
  29.         }   
  30.         return result;   
  31.     }   
  32.     protected static String format(int intval) {   
  33.         String formatted = Integer.toHexString(intval);   
  34.         StringBuffer buf = new StringBuffer("00000000");   
  35.         buf.replace(8 - formatted.length(), 8, formatted);   
  36.         return buf.toString();   
  37.     }   
  38.     protected static String format(short shortval) {   
  39.         String formatted = Integer.toHexString(shortval);   
  40.         StringBuffer buf = new StringBuffer("0000");   
  41.         buf.replace(4 - formatted.length(), 4, formatted);   
  42.         return buf.toString();   
  43.     }   
  44.     protected static int getJVM() {   
  45.         return JVM;   
  46.     }   
  47.     protected synchronized static short getCount() {   
  48.         if (counter < 0) {   
  49.             counter = 0;   
  50.         }   
  51.         return counter++;   
  52.     }   
  53.     protected static int getIP() {   
  54.         return IP;   
  55.     }   
  56.     protected static short getHiTime() {   
  57.         return (short) (System.currentTimeMillis() >>> 32);   
  58.     }   
  59.     protected static int getLoTime() {   
  60.         return (int) System.currentTimeMillis();   
  61.     }   
  62.     public static String generate() {   
  63.         return new StringBuffer(36).append(format(getIP())).append(sep).append(format(getJVM())).append(sep)   
  64.                 .append(format(getHiTime())).append(sep).append(format(getLoTime())).append(sep)   
  65.                 .append(format(getCount())).toString();   
  66.     }   
  67.     /**  
  68.      * @param args  
  69.      */   
  70.     public static void main(String[] args) {   
  71.         String id="";   
  72.         UUIDHexGenerator uuid = UUIDHexGenerator.getInstance();   
  73.         /*  
  74.         for (int i = 0; i < 100; i++) {  
  75.             id = uuid.generate();  
  76.         }*/   
  77.         id = uuid.generate();   
  78.         System.out.println(id);   
  79.     }   
  80. }   

转载出处:https://blog.csdn.net/qq_30641447/article/details/74907725

猜你喜欢

转载自blog.csdn.net/dinghuan2011/article/details/80428312