package com.stylefeng.guns.modular.account.util;
|
|
import com.thoughtworks.xstream.XStream;
|
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.lang.RandomStringUtils;
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.time.DateFormatUtils;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.*;
|
import java.lang.reflect.Field;
|
import java.util.*;
|
|
/**
|
* @program: publicpaydemo
|
* @description: 工具类
|
* @author: Mr.YS
|
* @CreatDate: 2019/5/17/017 13:24
|
*/
|
public class Util {
|
|
|
/**
|
* 获取到订单号
|
*
|
* @param msgSrcId 消息来源编号 1017
|
* @return 商户订单号 {来源编号(4位)}{时间(yyyyMMddmmHHssSSS)(17位)}{7位随机数}
|
*/
|
public static String getMerOrderId(String msgSrcId) {
|
return msgSrcId + DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS") + RandomStringUtils.randomNumeric(7);
|
}
|
|
/**
|
* 获取商户订单号
|
* @return
|
*/
|
public static String getMerchantOrderId() {
|
return DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS") + RandomStringUtils.randomNumeric(7);
|
}
|
|
/**
|
* 获取到退货订单号refundOrderId
|
*
|
* @param msgSrcId 消息来源编号 1017
|
* @return 商户订单号 {来源编号(4位)}{时间(yyyyMMddmmHHssSSS)(17位)}{7位随机数}
|
*/
|
public static String getRefundOrderId(String msgSrcId) {
|
return msgSrcId + DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS") + RandomStringUtils.randomNumeric(7);
|
}
|
|
|
|
// 获取HttpServletRequest里面的参数
|
public static Map<String, String> getRequestParams(HttpServletRequest request) {
|
Map<String, String[]> params = request.getParameterMap();
|
Map<String, String> params2 = new HashMap<>();
|
for (String key : params.keySet()) {
|
String[] values = params.get(key);
|
if (values.length > 0) {
|
params2.put(key, request.getParameter(key));
|
}
|
}
|
return params2;
|
}
|
|
|
public static String makeSign(String md5Key, Map<String, String> params) {
|
|
String preStr = buildSignString(params); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
|
String text = preStr + md5Key;
|
System.out.println("待签名字符串:"+text);
|
return DigestUtils.md5Hex(getContentBytes(text)).toUpperCase();
|
//return DigestUtils.sha256Hex(getContentBytes(text)).toLowerCase();
|
|
}
|
|
// 构建签名字符串
|
public static String buildSignString(Map<String, String> params) {
|
|
// params.put("Zm","test_test");
|
|
if (params == null || params.size() == 0) {
|
return "";
|
}
|
|
List<String> keys = new ArrayList<>(params.size());
|
|
for (String key : params.keySet()) {
|
if ("sign".equals(key))
|
continue;
|
if (StringUtils.isEmpty(params.get(key)))
|
continue;
|
keys.add(key);
|
}
|
//System.out.println(listToString(keys));
|
|
Collections.sort(keys);
|
|
StringBuilder buf = new StringBuilder();
|
|
for (int i = 0; i < keys.size(); i++) {
|
String key = keys.get(i);
|
String value = params.get(key);
|
|
if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
|
buf.append(key + "=" + value);
|
} else {
|
buf.append(key + "=" + value + "&");
|
}
|
}
|
|
return buf.toString();
|
}
|
|
// 根据编码类型获得签名内容byte[]
|
public static byte[] getContentBytes(String content) {
|
try {
|
return content.getBytes("UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException("签名过程中出现错误");
|
}
|
}
|
|
/**
|
* 通过反射的方式遍历对象的属性和属性值,方便调试
|
*
|
* @param o 要遍历的对象
|
* @throws Exception
|
*/
|
public static void reflect(Object o) throws Exception {
|
Class<? extends Object> 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) {
|
ByteArrayInputStream tInputStringStream = null;
|
if (sInputString != null && !sInputString.trim().equals("")) {
|
try {
|
tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("UTF-8"));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return tInputStringStream;
|
}
|
|
public static Object getObjectFromXML(String xml, Class<?> tClass) {
|
//将从API返回的XML数据映射到Java对象
|
XStream xStreamForResponseData = new XStream();
|
xStreamForResponseData.alias("xml", tClass);
|
xStreamForResponseData.ignoreUnknownElements();//暂时忽略掉一些新增的字段
|
return xStreamForResponseData.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){
|
System.out.println(log.toString());
|
return log.toString();
|
}
|
|
/**
|
* 读取本地的xml数据,一般用来自测用
|
* @param localPath 本地xml文件路径
|
* @return 读到的xml字符串
|
*/
|
public static String getLocalXMLString(String localPath) throws IOException {
|
return Util.inputStreamToString(Util.class.getResourceAsStream(localPath));
|
}
|
|
}
|