小程序微信小程序支付生成签名为什么body填汉字就会签名错位u

百姓网公众号微信扫码关注百姓网小程序微信扫扫立即体验扫码下载手机客户端免费抢油卡、红包、电影票|服务范围服务内容本市现已开通 、、、、、,点击城市名或切换城市即可访问 郑州赢科电子科技有限公司 金水3月21日 郑州赢科电子科技有限公司 金水3月21日10000元郑州码通软件 中原4月13日 河南网中网科技有限公司 金水3月7日268元 郑东4月14日  金水4月20日 冥王星科技 金水3月4日 河南网中网科技有限公司 金水2月28日1元河南网中网计算机科技有限...金水4月12日 河南网中网科技有限公司 金水3月7日 河南网中网科技有限公司 金水2月28日1099元郑州太平洋网络科技有限公司 二七4月14日1元广州市环德信息科技有限公司 高新区4月12日  上街4月14日 河南网中网科技有限公司 金水2月28日 河南网中网科技有限公司 金水3月1日 登封信悦网络科技有限公司 登封2月23日 冥王星科技 金水2月27日 河南为之享计算机科技有限...金水4月25日1元广州市环德信息科技有限公司 高新区4月12日  金水2月24日 河南网中网科技有限公司 金水3月6日  金水3月19日  新郑3月13日 河南网中网科技有限公司 金水3月1日  登封2月27日 郑州燚轩软件科技有限公司 中原4月17日268元 郑东4月20日  金水3月9日2000元河南航迪软件科技有限公司 金水4月7日 登封信悦网络科技有限公司 登封3月18日268元 郑东4月12日 河南网中网科技有限公司 金水2月28日  新密3月15日999元郑州泽海科技有限公司 金水5月15日 河南网中网科技有限公司 金水3月1日10元郑州软银科技有限公司 管城4月5日  郑东新区4月20日  金水3月31日999元郑州太平洋网络科技有限公司 二七4月18日10000元郑州码通软件 中原4月1日  新郑3月7日1元广州市环德信息科技有限公司 周边县市4月12日1元广州市环德信息科技有限公司 高新区4月12日  登封3月9日  高新区3月8日2000元河南航迪软件科技有限公司 金水4月7日1000元河南网中网计算机科技有限...金水4月23日268元 郑东4月18日|||||||||沪公网安备16号0&G:16&GM:159
描述:请填写描述手机号:请填写手机号请填写手机号上传图片:打开微信,扫一扫右侧二维码,即可完成绑定 -->绑定后,您可以:1. 立即在手机上收到用户给您的留言2. 使用手机快速完成付费推广的续费动作3. 第一时间了解到百姓网付费推广最新的促销活动,以及享受微信端独特的促销活动4. 更快速地将信息通过微信分享给好友、同事、朋友圈5. 如果您是招聘类目用户,还能够第一时间接收到新简历通知下载APP无需登录实时接受私信提醒,联系更便捷!或点击下方先登录再进入私信联系微信小程序支付(java后端) - 追着太阳晒
微信小程序支付(java后端) - 追着太阳晒
编辑:www.fx114.net
本篇文章主要介绍了"微信小程序支付(java后端) - 追着太阳晒 ",主要涉及到微信小程序支付(java后端) - 追着太阳晒 方面的内容,对于微信小程序支付(java后端) - 追着太阳晒 感兴趣的同学可以参考一下。
&第一步&进入小程序,下单,请求下单支付,调用小程序登录API来获取Openid(https://mp./debug/w ... .html#wxloginobject),生成商户订单,这些都是在小程序端完成的业务。&小程序端代码&// pages/pay/pay.js&var app = getApp();&Page({&&&& data: {},&&&& onLoad: function (options) {&&&&&&&& // 页面初始化 options为页面跳转所带来的参数&&&& },&&&& /* 微信支付 */&&&& wxpay: function () {&&&&&&&& var that = this&&&&&&&& //登陆获取code&&&&&&&& wx.login({&&&&&&&&&&&& success: function (res) {&&&&&&&&&&&&&&&& console.log(res.code)&&&&&&&&&&&&&&&& //获取openid&&&&&&&&&&&&&&&& that.getOpenId(res.code)&&&&&&&&&&&& }&&&&&&&& });&&&& },&&&& getOpenId: function (code) {&&&&&&&& var that =&&&&&&&& wx.request({&&&&&&&&&&&& url: "https://api./sns/jscode2session?appid=wxae496c&secret=5d6a7dc60f84f7aa85253c&js_code=" + code + "&grant_type=authorization_code",&&&&&&&&&&&& data: {},&&&&&&&&&&&& method: 'GET',&&&&&&&&&&&& success: function (res) {&&&&&&&&&&&&&&&& console.log('返回openId')&&&&&&&&&&&&&&&& console.log(res.data)&&&&&&&&&&&&&&&& that.generateOrder(res.data.openid)&&&&&&&&&&&& },&&&&&&&&&&&& fail: function () {&&&&&&&&&&&&&&&& // fail&&&&&&&&&&&& },&&&&&&&&&&&& complete: function () {&&&&&&&&&&&&&&&& // complete&&&&&&&&&&&& }&&&&&&&& })&&&& },&&&& /**生成商户订单 */&&&& generateOrder: function (openid) {&&&&&&&& var that = this&&&&&&&& //统一支付&&&&&&&& wx.request({&&&&&&&&&&&& url: 'http://localhost:8070/RMS/pay_pay.action',&&&&&&&&&&&& method: 'GET',&&&&&&&&&&&& data: {&&&&&&&&&&&&&&&& total_fee: '5',&&&&&&&&&&&&&&&& body: '支付测试',&&&&&&&&&&&&&&&& attach:'真假酒水'&&&&&&&&&&&& },&&&&&&&&&&&& success: function (res) {&&&&&&&&&&&&&&&& console.log(res)&&&&&&&&&&&&&&&& var pay = res.data&&&&&&&&&&&&&&&& //发起支付&&&&&&&&&&&&&&&& var timeStamp = pay[0].timeS&&&&&&&&&&&&&&&& console.log("timeStamp:"+timeStamp)&&&&&&&&&&&&&&&& var packages = pay[0].&&&&&&&&&&&&&&&& console.log("package:"+packages)&&&&&&&&&&&&&&&& var paySign = pay[0].payS&&&&&&&&&&&&&&&& console.log("paySign:"+paySign)&&&&&&&&&&&&&&&& var nonceStr = pay[0].nonceS&&&&&&&&&&&&&&&&& console.log("nonceStr:"+nonceStr)&&&&&&&&&&&&&&&& var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr };&&&&&&&&&&&&&&&& that.pay(param)&&&&&&&&&&&& },&&&&&&&& })&&&& },&&&& /* 支付&& */&&&& pay: function (param) {&&&&&&&& console.log("支付")&&&&&&&& console.log(param)&&&&&&&& wx.requestPayment({&&&&&&&&&&&& timeStamp: param.timeStamp,&&&&&&&&&&&& nonceStr: param.nonceStr,&&&&&&&&&&&& package: param.package,&&&&&&&&&&&& signType: param.signType,&&&&&&&&&&&& paySign: param.paySign,&&&&&&&&&&&& success: function (res) {&&&&&&&&&&&&&&&& // success&&&&&&&&&&&&&&&& console.log("支付")&&&&&&&&&&&&&&&& console.log(res)&&&&&&&&&&&&&&&& wx.navigateBack({&&&&&&&&&&&&&&&&&&&& delta: 1, // 回退前 delta(默认为1) 页面&&&&&&&&&&&&&&&&&&&& success: function (res) {&&&&&&&&&&&&&&&&&&&&&&&& wx.showToast({&&&&&&&&&&&&&&&&&&&&&&&&&&&& title: '支付成功',&&&&&&&&&&&&&&&&&&&&&&&&&&&& icon: 'success',&&&&&&&&&&&&&&&&&&&&&&&&&&&& duration: 2000&&&&&&&&&&&&&&&&&&&&&&&& })&&&&&&&&&&&&&&&&&&&& },&&&&&&&&&&&&&&&&&&&& fail: function () {&&&&&&&&&&&&&&&&&&&&&&&& // fail&&&&&&&&&&&&&&&&&&&& },&&&&&&&&&&&&&&&&&&&& complete: function () {&&&&&&&&&&&&&&&&&&&&&&&& // complete&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&& })&&&&&&&&&&&& },&&&&&&&&&&&& fail: function (res) {&&&&&&&&&&&&&&&& // fail&&&&&&&&&&&&&&&& console.log("支付失败")&&&&&&&&&&&&&&&& console.log(res)&&&&&&&&&&&& },&&&&&&&&&&&& complete: function () {&&&&&&&&&&&&&&&& // complete&&&&&&&&&&&&&&&& console.log("pay complete")&&&&&&&&&&&& }&&&&&&&& })&&&& }&})&复制代码&第二步&调用支付统一下单API来获取prepay_id,并将小程序调起支付数据需要签名的字段appId,timeStamp,nonceStr,package再次签名(https://pay./wiki/d ... ter=7_7&index=3)&后台代码&package cn.it.shop.&import java.io.ByteArrayInputS&import java.io.InputS&import java.io.UnsupportedEncodingE&import java.text.SimpleDateF&import java.util.D&import java.util.HashM&import java.util.L&import java.util.M&import org.dom4j.D&import org.dom4j.DocumentE&import org.dom4j.E&import org.dom4j.io.SAXR&import cn.it.shop.util.MessageU&import cn.it.shop.util.PayU&import cn.it.shop.util.PaymentPo;&import cn.it.shop.util.UUIDHexG&import net.sf.json.JSONA&import net.sf.json.JSONO&/**&* @author&* @version 创建时间:日 下午4:59:03&* 小程序端请求的后台action,返回签名后的数据传到前台&*/&public class PayAction {&&&& private String total_//总金额&&&& private S//商品描述&&&& private S//商品详情&&&&&&& private S//附加数据&&&& private String time_//交易起始时间&&&& private String time_//交易结束时间&&&& private S//用户标识&&&& private JSONArray jsonArray=new JSONArray();&&&& public String pay() throws UnsupportedEncodingException, DocumentException{&&&&&&&&& body = new String(body.getBytes("UTF-8"),"ISO-8859-1");&&&&&&&&& String appid = "替换为自己的小程序ID";//小程序ID&&&&&&&& String mch_id = "替换为自己的商户号";//商户号&&&&&&&& String nonce_str = UUIDHexGenerator.generate();//随机字符串&&&&&&&& String today = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());&&&&&&&& String code = PayUtil.createCode(8);&&&&&&&& String out_trade_no = mch_id+today+//商户订单号&&&&&&&& String spbill_create_ip = "替换为自己的终端IP";//终端IP&&&&&&&& String notify_url = "http://www./wxpay/pay.php";//通知地址&&&&&&&& String trade_type = "JSAPI";//交易类型&&&&&&&&& String openid="替换为用户的openid";//用户标识&&&&&&&& /**/&&&&&&&& PaymentPo paymentPo = new PaymentPo();&&&&&&&& paymentPo.setAppid(appid);&&&&&&&& paymentPo.setMch_id(mch_id);&&&&&&&& paymentPo.setNonce_str(nonce_str);&&&&&&&& String newbody=new String(body.getBytes("ISO-8859-1"),"UTF-8");//以utf-8编码放入paymentPo,微信支付要求字符编码统一采用UTF-8字符编码&&&&&&&& paymentPo.setBody(newbody);&&&&&&&& paymentPo.setOut_trade_no(out_trade_no);&&&&&&&& paymentPo.setTotal_fee(total_fee);&&&&&&&& paymentPo.setSpbill_create_ip(spbill_create_ip);&&&&&&&& paymentPo.setNotify_url(notify_url);&&&&&&&& paymentPo.setTrade_type(trade_type);&&&&&&&& paymentPo.setOpenid(openid);&&&&&&&& // 把请求参数打包成数组&&&&&&&& Map sParaTemp = new HashMap();&&&&&&&& sParaTemp.put("appid", paymentPo.getAppid());&&&&&&&& sParaTemp.put("mch_id", paymentPo.getMch_id());&&&&&&&& sParaTemp.put("nonce_str", paymentPo.getNonce_str());&&&&&&&& sParaTemp.put("body",& paymentPo.getBody());&&&&&&&& sParaTemp.put("out_trade_no", paymentPo.getOut_trade_no());&&&&&&&& sParaTemp.put("total_fee",paymentPo.getTotal_fee());&&&&&&&& sParaTemp.put("spbill_create_ip", paymentPo.getSpbill_create_ip());&&&&&&&& sParaTemp.put("notify_url",paymentPo.getNotify_url());&&&&&&&& sParaTemp.put("trade_type", paymentPo.getTrade_type());&&&&&&&& sParaTemp.put("openid", paymentPo.getOpenid());&&&&&&&& // 除去数组中的空值和签名参数&&&&&&&& Map sPara = PayUtil.paraFilter(sParaTemp);&&&&&&&& String prestr = PayUtil.createLinkString(sPara); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串&&&&&&&& String key = "&key=替换为商户支付密钥"; // 商户支付密钥&&&&&&&& //MD5运算生成签名&&&&&&&& String mysign = PayUtil.sign(prestr, key, "utf-8").toUpperCase();&&&&&&&& paymentPo.setSign(mysign);&&&&&&&& //打包要发送的xml&&&&&&&& String respXml = MessageUtil.messageToXML(paymentPo);&&&&&&&& // 打印respXml发现,得到的xml中有“__”不对,应该替换成“_”&&&&&&&& respXml = respXml.replace("__", "_");&&&&&&&& String url = "https://api.mch./pay/unifiedorder";//统一下单API接口链接&&&&&&&& String param = respX&&&&&&&& //String result = SendRequestForUrl.sendRequest(url, param);//发起请求&&&&&&&& String result =PayUtil.httpRequest(url, "POST", param);&&&&&&&& // 将解析结果存储在HashMap中&&&&&&&& Map map = new HashMap();&&&&&&&&& InputStream in=new ByteArrayInputStream(result.getBytes());&&&&&&&&& // 读取输入流&&&&&&&& SAXReader reader = new SAXReader();&&&&&&&& Document document = reader.read(in);&&&&&&&& // 得到xml根元素&&&&&&&& Element root = document.getRootElement();&&&&&&&& // 得到根元素的所有子节点&&&&&&&& @SuppressWarnings("unchecked")&&&&&&&& List elementList = root.elements();&&&&&&&& for (Element element : elementList) {&&&&&&&&&&&& map.put(element.getName(), element.getText());&&&&&&&& }&&&&&&&& // 返回信息&&&&&&&& String return_code = map.get("return_code");//返回状态码&&&&&&&& String return_msg = map.get("return_msg");//返回信息&&&&&&&& System.out.println("return_msg"+return_msg);&&&&&&&& JSONObject JsonObject=new JSONObject() ;&&&&&&&& if(return_code=="SUCCESS"||return_code.equals(return_code)){&&&&&&&&&&&& // 业务结果&&&&&&&&&&&& String prepay_id = map.get("prepay_id");//返回的预付单信息&&&&&&&&&&&& String nonceStr=UUIDHexGenerator.generate();&&&&&&&&&&&& JsonObject.put("nonceStr", nonceStr);&&&&&&&&&&&& JsonObject.put("package", "prepay_id="+prepay_id);&&&&&&&&&&&& Long timeStamp= System.currentTimeMillis()/1000;&&&&&&&&&&&& JsonObject.put("timeStamp", timeStamp+"");&&&&&&&&&&&& String stringSignTemp = "appId="+appid+"&nonceStr=" + nonceStr + "&package=prepay_id=" + prepay_id+ "&signType=MD5&timeStamp=" + timeS&&&&&&&&&&&& //再次签名&&&&&&&&&&&& String paySign=PayUtil.sign(stringSignTemp, "&key=替换为自己的密钥", "utf-8").toUpperCase();&&&&&&&&&&&& JsonObject.put("paySign", paySign);&&&&&&&&&&&& jsonArray.add(JsonObject);&&&&&&&& }&&&&&&&& return "pay";&&&& }&&&& public String getTotal_fee() {&&&&&&&& return total_&&&& }&&&& public void setTotal_fee(String total_fee) {&&&&&&&& this.total_fee = total_&&&& }&&&& public String getBody() {&&&&&&&&&&&& }&&&& public void setBody(String body) {&&&&&&&& this.body =&&&& }&&&& public JSONArray getJsonArray() {&&&&&&&& return jsonA&&&& }&&&& public void setJsonArray(JSONArray jsonArray) {&&&&&&&& this.jsonArray = jsonA&&&& }&&&& public String getDetail() {&&&&&&&&&&&& }&&&& public void setDetail(String detail) {&&&&&&&& this.detail =&&&& }&&&& public String getAttach() {&&&&&&&&&&&& }&&&& public void setAttach(String attach) {&&&&&&&& this.attach =&&&& }&&&& public String getTime_start() {&&&&&&&& return time_&&&& }&&&& public void setTime_start(String time_start) {&&&&&&&& this.time_start = time_&&&& }&&&& public String getTime_expire() {&&&&&&&& return time_&&&& }&&&& public void setTime_expire(String time_expire) {&&&&&&&& this.time_expire = time_&&&& }&&&& public String getOpenid() {&&&&&&&&&&&& }&&&& public void setOpenid(String openid) {&&&&&&&& this.openid =&&&& }&}&复制代码&后台业务逻辑涉及到的工具类及参数封装类&MessageUtil&package cn.it.shop.&import java.io.IOE&import java.io.W&import java.util.HashM&import java.util.L&import javax.servlet.http.HttpServletR&import org.dom4j.D&import org.dom4j.E&import org.dom4j.io.SAXR&import com.thoughtworks.xstream.XS&import com.thoughtworks.xstream.core.util.QuickW&import com.thoughtworks.xstream.io.HierarchicalStreamW&import com.thoughtworks.xstream.io.xml.PrettyPrintW&import com.thoughtworks.xstream.io.xml.XppD&public class MessageUtil {&&&& public static HashMap parseXML(HttpServletRequest request) throws Exception, IOException{&&&&&&&& HashMap map=new HashMap();&&&&&&&& // 通过IO获得Document&&&&&&&& SAXReader reader = new SAXReader();&&&&&&&& Document doc = reader.read(request.getInputStream());&&&&&&&& //得到xml的根节点&&&&&&&& Element root=doc.getRootElement();&&&&&&&& recursiveParseXML(root,map);&&&&&&&&&&&& }&&&& private static void recursiveParseXML(Element root,HashMap map){&&&&&&&& //得到根节点的子节点列表&&&&&&&& List elementList=root.elements();&&&&&&&& //判断有没有子元素列表&&&&&&&& if(elementList.size()==0){&&&&&&&&&&&& map.put(root.getName(), root.getTextTrim());&&&&&&&& }&&&&&&&& else{&&&&&&&&&&&& //遍历&&&&&&&&&&&& for(Element e:elementList){&&&&&&&&&&&&&&&& recursiveParseXML(e,map);&&&&&&&&&&&& }&&&&&&&& }&&&& }&&&& private static XStream xstream = new XStream(new XppDriver() {&&&&&&&& public HierarchicalStreamWriter createWriter(Writer out) {&&&&&&&&&&&& return new PrettyPrintWriter(out) {&&&&&&&&&&&&&&&& // 对所有xml节点都增加CDATA标记&&&&&&&&&&&&&&&& boolean cdata =&&&&&&&&&&&&&&&& public void startNode(String name, Class clazz) {&&&&&&&&&&&&&&&&&&&& super.startNode(name, clazz);&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&& protected void writeText(QuickWriter writer, String text) {&&&&&&&&&&&&&&&&&&&& if (cdata) {&&&&&&&&&&&&&&&&&&&&&&&& writer.write("&&&&&&&&&&&&&&&&&&&&&&& writer.write(text);&&&&&&&&&&&&&&&&&&&&&&&& writer.write("]]&");&&&&&&&&&&&&&&&&&&&& } else {&&&&&&&&&&&&&&&&&&&&&&&& writer.write(text);&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&& }&&&&&&&&&&&& };&&&&&&&& }&&&& });&&&& public static String messageToXML(PaymentPo paymentPo){&&&&&&&& xstream.alias("xml",PaymentPo.class);&&&&&&&& return xstream.toXML(paymentPo);&&&& }&}&PaymentPo//封装支付参数实体&package cn.it.shop.&/**&* @author&* @version 创建时间:日 下午4:20:52&* 类说明&*/&public class PaymentPo {&&&& private S//小程序ID&&&& private String mch_//商户号&&&& private String device_//设备号&&&& private String nonce_//随机字符串&&&& private S//签名&&&& private S//商品描述&&&&& private S//商品详情&&&&&&& private S//附加数据&&&& private String out_trade_//商户订单号&&&& private String fee_//货币类型&&&& private String spbill_create_//终端IP&&&& private String time_//交易起始时间&&&& private String time_//交易结束时间&&&& private String goods_//商品标记&&&& private String total_//总金额&&&& private String notify_//通知地址&&&&&&& private String trade_//交易类型&&&&&&& private String limit_//指定支付方式&&&& private S//用户标识&&&& public String getAppid() {&&&&&&&&&&&& }&&&& public void setAppid(String appid) {&&&&&&&& this.appid =&&&& }&&&& public String getMch_id() {&&&&&&&& return mch_&&&& }&&&& public void setMch_id(String mch_id) {&&&&&&&& this.mch_id = mch_&&&& }&&&& public String getNonce_str() {&&&&&&&& return nonce_&&&& }&&&& public void setNonce_str(String nonce_str) {&&&&&&&& this.nonce_str = nonce_&&&& }&&&& public String getSign() {&&&&&&&&&&&& }&&&& public void setSign(String sign) {&&&&&&&& this.sign =&&&& }&&&& public String getBody() {&&&&&&&&&&&& }&&&& public void setBody(String body) {&&&&&&&& this.body =&&&& }&&&& public String getOut_trade_no() {&&&&&&&& return out_trade_&&&& }&&&& public void setOut_trade_no(String out_trade_no) {&&&&&&&& this.out_trade_no = out_trade_&&&& }&&&& public String getTotal_fee() {&&&&&&&& return total_&&&& }&&&& public void setTotal_fee(String total_fee) {&&&&&&&& this.total_fee = total_&&&& }&&&& public String getNotify_url() {&&&&&&&& return notify_&&&& }&&&& public void setNotify_url(String notify_url) {&&&&&&&& this.notify_url = notify_&&&& }&&&& public String getTrade_type() {&&&&&&&& return trade_&&&& }&&&& public void setTrade_type(String trade_type) {&&&&&&&& this.trade_type = trade_&&&& }&&&& public String getOpenid() {&&&&&&&&&&&& }&&&& public void setOpenid(String openid) {&&&&&&&& this.openid =&&&& }&&&& public String getSpbill_create_ip() {&&&&&&&& return spbill_create_&&&& }&&&& public void setSpbill_create_ip(String spbill_create_ip) {&&&&&&&& this.spbill_create_ip = spbill_create_&&&& }&&&& public String getDevice_info() {&&&&&&&& return device_&&&& }&&&& public void setDevice_info(String device_info) {&&&&&&&& this.device_info = device_&&&& }&&&& public String getDetail() {&&&&&&&&&&&& }&&&& public void setDetail(String detail) {&&&&&&&& this.detail =&&&& }&&&& public String getAttach() {&&&&&&&&&&&& }&&&& public void setAttach(String attach) {&&&&&&&& this.attach =&&&& }&&&& public String getFee_type() {&&&&&&&& return fee_&&&& }&&&& public void setFee_type(String fee_type) {&&&&&&&& this.fee_type = fee_&&&& }&&&& public String getTime_start() {&&&&&&&& return time_&&&& }&&&& public void setTime_start(String time_start) {&&&&&&&& this.time_start = time_&&&& }&&&& public String getTime_expire() {&&&&&&&& return time_&&&& }&&&& public void setTime_expire(String time_expire) {&&&&&&&& this.time_expire = time_&&&& }&&&& public String getGoods_tag() {&&&&&&&& return goods_&&&& }&&&& public void setGoods_tag(String goods_tag) {&&&&&&&& this.goods_tag = goods_&&&& }&&&& public String getLimit_pay() {&&&&&&&& return limit_&&&& }&&&& public void setLimit_pay(String limit_pay) {&&&&&&&& this.limit_pay = limit_&&&& }&}&PayUtil&package cn.it.shop.&import java.io.BufferedR&import java.io.InputS&import java.io.InputStreamR&import java.io.OutputS&import java.io.UnsupportedEncodingE&import java.net.HttpURLC&import java.net.URL;&import java.util.ArrayL&import java.util.C&import java.util.HashM&import java.util.L&import java.util.M&import mons.codec.digest.DigestU&/**&* @author&* @version 创建时间:日 下午7:46:29 类说明&*/&public class PayUtil {&&&& /**&&&&& * 签名字符串&&&&& * @param text需要签名的字符串&&&&& * @param key 密钥&&&&& * @param input_charset编码格式&&&&& *&&签名结果&&&&& */&&&& public static String sign(String text, String key, String input_charset) {&&&&&&&& text = text +&&&&&&&& return DigestUtils.md5Hex(getContentBytes(text, input_charset));&&&& }&&&& /**&&&&& * 签名字符串&&&&& *& @param text需要签名的字符串&&&&& * @param sign 签名结果&&&&& * @param key密钥&&&&& * @param input_charset 编码格式&&&&& * @return 签名结果&&&&& */&&&& public static boolean verify(String text, String sign, String key, String input_charset) {&&&&&&&& text = text +&&&&&&&& String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));&&&&&&&& if (mysign.equals(sign)) {&&&&&&&&&&&&&&&&&&&& } else {&&&&&&&&&&&&&&&&&&&& }&&&& }&&&& /**&&&&& * @param content&&&&& * @param charset&&&&& * @return&&&&& * @throws SignatureException&&&&& * @throws UnsupportedEncodingException&&&&& */&&&& public static byte[] getContentBytes(String content, String charset) {&&&&&&&& if (charset == null || "".equals(charset)) {&&&&&&&&&&&& return content.getBytes();&&&&&&&& }&&&&&&&& try {&&&&&&&&&&&& return content.getBytes(charset);&&&&&&&& } catch (UnsupportedEncodingException e) {&&&&&&&&&&&& throw new RuntimeException("MD5签名过程◇◇现错误,指定的编码集不对,您目前指定的编码集是:" + charset);&&&&&&&& }&&&& }&&&& /**&&&&& * 生成6位或10位随机数 param codeLength(多少位)&&&&& * @return&&&&& */&&&& public static String createCode(int codeLength) {&&&&&&&& String code = "";&&&&&&&& for (int i = 0; i & codeL i++) {&&&&&&&&&&&& code += (int) (Math.random() * 9);&&&&&&&& }&&&&&&&&&&&& }&&&& private static boolean isValidChar(char ch) {&&&&&&&& if ((ch &= '0' && ch &= '9') || (ch &= 'A' && ch &= 'Z') || (ch &= 'a' && ch &= 'z'))&&&&&&&&&&&&&&&&&&&& if ((ch &= 0x4e00 && ch &= 0x7fff) || (ch &= 0x8000 && ch &= 0x952f))&&&&&&&&&&&&// 简体中文汉字编码&&&&&&&&&&&& }&&&& /**&&&&& * 除去数组中的空值和签名参数&&&&& * @param sArray 签名参数组&&&&& * @return 去掉空值与签名参数后的新签名参数组&&&&& */&&&& public static Map paraFilter(Map sArray) {&&&&&&&& Map result = new HashMap();&&&&&&&& if (sArray == null || sArray.size() &= 0) {&&&&&&&&&&&&&&&&&&&& }&&&&&&&& for (String key : sArray.keySet()) {&&&&&&&&&&&& String value = sArray.get(key);&&&&&&&&&&&& if (value == null || value.equals("") || key.equalsIgnoreCase("sign")&&&&&&&&&&&&&&&&&&&& || key.equalsIgnoreCase("sign_type")) {&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&& result.put(key, value);&&&&&&&& }&&&&&&&&&&&& }&&&& /**&&&&& * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串&&&&& * @param params 需要排序并参与字符拼接的参数组&&&&& * @return 拼接后字符串&&&&& */&&&& public static String createLinkString(Map params) {&&&&&&&& List keys = new ArrayList(params.keySet());&&&&&&&& Collections.sort(keys);&&&&&&&& String prestr = "";&&&&&&&& for (int i = 0; i & keys.size(); i++) {&&&&&&&&&&&& String key = keys.get(i);&&&&&&&&&&&& String value = params.get(key);&&&&&&&&&&&& if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符&&&&&&&&&&&&&&&& prestr = prestr + key + "=" +&&&&&&&&&&&& } else {&&&&&&&&&&&&&&&& prestr = prestr + key + "=" + value + "&";&&&&&&&&&&&& }&&&&&&&& }&&&&&&&&&&&& }&&&& /**&&&&& *&&&&& * @param requestUrl请求地址&&&&& * @param requestMethod请求方法&&&&& * @param outputStr参数&&&&& */&&&& public static String httpRequest(String requestUrl,String requestMethod,String outputStr){&&&&&&&& // 创建SSLContext&&&&&&&& StringBuffer buffer=&&&&&&&& try{&&&&&&&& URL url = new URL(requestUrl);&&&&&&&& HttpURLConnection conn = (HttpURLConnection) url.openConnection();&&&&&&&& conn.setRequestMethod(requestMethod);&&&&&&&& conn.setDoOutput(true);&&&&&&&& conn.setDoInput(true);&&&&&&&& conn.connect();&&&&&&&& //往服务器端写内容&&&&&&&& if(null !=outputStr){&&&&&&&&&&&& OutputStream os=conn.getOutputStream();&&&&&&&&&&&& os.write(outputStr.getBytes("utf-8"));&&&&&&&&&&&& os.close();&&&&&&&& }&&&&&&&& // 读取服务器端返回的内容&&&&&&&& InputStream is = conn.getInputStream();&&&&&&&& InputStreamReader isr = new InputStreamReader(is, "utf-8");&&&&&&&& BufferedReader br = new BufferedReader(isr);&&&&&&&& buffer = new StringBuffer();&&&&&&&& String line =&&&&&&&& while ((line = br.readLine()) != null) {&&&&&&&&&&&&&&&&&&&&&& buffer.append(line);&&&&&&&& }&&&&&&&& }catch(Exception e){&&&&&&&&&&&& e.printStackTrace();&&&&&&&& }&&&&&&&& return buffer.toString();&&&&&&&& }&&&&&& public static String urlEncodeUTF8(String source){&&&&&&&& String result=&&&&&&&& try {&&&&&&&&&&&& result=java.net.URLEncoder.encode(source, "UTF-8");&&&&&&&& } catch (UnsupportedEncodingException e) {&&&&&&&&&&&& // TODO Auto-generated catch block&&&&&&&&&&&& e.printStackTrace();&&&&&&&& }&&&&&&&&&&&& }&}&UUIDHexGenerator&package cn.it.shop.&import java.net.InetA&/**&* @author&* @version 创建时间:日 下午7:45:06 类说明&*/&public class UUIDHexGenerator {&&&& private static String sep = "";&&&& private static final int IP;&&&& private static short counter = (short) 0;&&&& private static final int JVM = (int) (System.currentTimeMillis() &&&&;&&&& private static UUIDHexGenerator uuidgen = new UUIDHexGenerator();&&&& static {&&&&&&&&&&&&&&&& try {&&&&&&&&&&&& ipadd = toInt(InetAddress.getLocalHost().getAddress());&&&&&&&& } catch (Exception e) {&&&&&&&&&&&& ipadd = 0;&&&&&&&& }&&&&&&&& IP =&&&& }&&&& public static UUIDHexGenerator getInstance() {&&&&&&&&&&&& }&&&& public static int toInt(byte[] bytes) {&&&&&&&& int result = 0;&&&&&&&& for (int i = 0; i & 4; i++) {&&&&&&&&&&&& result = (result &&&&- Byte.MIN_VALUE + (int)&&&&&&&& }&&&&&&&&&&&& }&&&& protected static String format(int intval) {&&&&&&&& String formatted = Integer.toHexString(intval);&&&&&&&& StringBuffer buf = new StringBuffer("");&&&&&&&& buf.replace(8 - formatted.length(), 8, formatted);&&&&&&&& return buf.toString();&&&& }&&&& protected static String format(short shortval) {&&&&&&&& String formatted = Integer.toHexString(shortval);&&&&&&&& StringBuffer buf = new StringBuffer("0000");&&&&&&&& buf.replace(4 - formatted.length(), 4, formatted);&&&&&&&& return buf.toString();&&&& }&&&& protected static int getJVM() {&&&&&&&& return JVM;&&&& }&&&& protected synchronized static short getCount() {&&&&&&&& if (counter & 0) {&&&&&&&&&&&& counter = 0;&&&&&&&& }&&&&&&&& return counter++;&&&& }&&&& protected static int getIP() {&&&&&&&& return IP;&&&& }&&&& protected static short getHiTime() {&&&&&&&& return (short) (System.currentTimeMillis() &&& 32);&&&& }&&&& protected static int getLoTime() {&&&&&&&& return (int) System.currentTimeMillis();&&&& }&&&& public static String generate() {&&&&&&&& return new StringBuffer(36).append(format(getIP())).append(sep).append(format(getJVM())).append(sep)&&&&&&&&&&&&&&&& .append(format(getHiTime())).append(sep).append(format(getLoTime())).append(sep)&&&&&&&&&&&&&&&& .append(format(getCount())).toString();&&&& }&&&& /**&&&&& * @param args&&&&& */&&&& public static void main(String[] args) {&&&&&&&& String id="";&&&&&&&& UUIDHexGenerator uuid = UUIDHexGenerator.getInstance();&&&&&&&& /*&&&&&&&& for (int i = 0; i & 100; i++) {&&&&&&&&&&&& id = uuid.generate();&&&&&&&& }*/&&&&&&&& id = uuid.generate();&&&&&&&& System.out.println(id);&&&& }&}&转自:/doc/topic-.html
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:}

我要回帖

更多关于 微信小程序 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信