package com.demo;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.demo.utils.MD5Util;
public class Method {
/**
* 随机数生成算法
*
* @param length
* int 设定所取出随机数的长度。length小于11
* @return int 返回生成的随机数。
*/
public static int buildRandom(int length) {
int num = 1;
double random = Math.random();
if (random < 0.1) {
random = random + 0.1;
}
for (int i = 0; i < length; i++) {
num = num * 10;
}
return (int) ((random * num));
}
/**
* 获取当前时间 yyyyMMddHHmmss
*
* @return String
*/
public static String getCurrTime() {
Date now = new Date();
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String s = outFormat.format(now);
return s;
}
/**
* 创建sign签名
*
*/
public static String createSign(String characterEncoding, Map<Object, String> packageParams, String API_KEY) {
StringBuffer sb = new StringBuffer();
Set es = packageParams.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + API_KEY);
String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
return sign;
}
/**
* @author
* @date 2016-4-22
* @Description:将请求参数转换为xml格式的string
* @param parameters
* 请求参数
* @return
*/
public static String getRequestXml(Map<Object, String> parameters) {
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
Set es = parameters.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
// sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
sb.append("<" + k + ">" + v + "</" + k + ">");
} else {
sb.append("<" + k + ">" + v + "</" + k + ">");
}
}
sb.append("</xml>");
return String.valueOf(sb);
}
/**
* 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
* @return boolean
*/
public static boolean isTenpaySign(String characterEncoding, Map<Object, String> packageParams, String API_KEY) {
StringBuffer sb = new StringBuffer();
Set es = packageParams.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
String v = (String)entry.getValue();
if(!"sign".equals(k) && null != v && !"".equals(v)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + API_KEY);
//算出摘要
String mysign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toLowerCase();
String tenpaySign = ((String)packageParams.get("sign")).toLowerCase();
//System.out.println(tenpaySign + " " + mysign);
return tenpaySign.equals(mysign);
}
/**
* 获取本机Ip
*
* 通过 获取系统所有的networkInterface网络接口 然后遍历 每个网络下的InterfaceAddress组。
* 获得符合 <code>InetAddress instanceof Inet4Address</code> 条件的一个IpV4地址
* @return
*/
@SuppressWarnings("rawtypes")
public static String localIp(){
String ip = null;
Enumeration allNetInterfaces;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
for (InterfaceAddress add : InterfaceAddress) {
InetAddress Ip = add.getAddress();
if (Ip != null && Ip instanceof Inet4Address) {
ip = Ip.getHostAddress();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
System.out.println("获取本机Ip失败:异常信息:"+e.getMessage());
}
return ip;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
微信刷卡支付(被刷支付)
共45个文件
class:10个
java:10个
jar:9个
5星 · 超过95%的资源 需积分: 9 98 下载量 105 浏览量
2016-11-04
14:16:13
上传
评论 2
收藏 1.4MB ZIP 举报
温馨提示
http://blog.csdn.net/qq_22778717/article/details/53034268
资源推荐
资源详情
资源评论
收起资源包目录
ScanPay.zip (45个子文件)
ScanPay
ScanPay
src
com
demo
dao
QueryDao.java 2KB
PayDao.java 2KB
Method.java 6KB
utils
XMLUtil.java 2KB
MD5Util.java 1KB
ClientCustomSSL.java 5KB
HttpUtil.java 2KB
wechatscan.java 4KB
wechatquery.java 3KB
wechatcancel.java 2KB
.classpath 532B
.mymetadata 297B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 69B
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.ws.prefs 78B
org.eclipse.wst.common.component 465B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
.myeclipse
WebContent
META-INF
MANIFEST.MF 36B
index2.jsp 2KB
index.jsp 4KB
query.jsp 1KB
WEB-INF
classes
com
demo
Method.class 5KB
dao
PayDao.class 3KB
QueryDao.class 2KB
wechatscan.class 5KB
utils
MD5Util.class 2KB
ClientCustomSSL.class 5KB
HttpUtil.class 3KB
XMLUtil.class 3KB
wechatquery.class 3KB
wechatcancel.class 2KB
lib
commons-logging-1.1.1.jar 59KB
jdom-1.0.jar 150KB
httpclient-4.3.4.jar 576KB
commons-logging.jar 31KB
fluent-hc-4.3.4.jar 29KB
httpcore-4.3.2.jar 276KB
httpmime-4.3.4.jar 36KB
commons-codec-1.7.jar 254KB
httpclient-cache-4.3.4.jar 146KB
web.xml 404B
.project 1KB
共 45 条
- 1
资源评论
- weixingbo2018-09-19学习研究一下
ouyuyao
- 粉丝: 43
- 资源: 76
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功