You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.dsideal.gw.Util;
import cn.hutool.core.io.IoUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
@SuppressWarnings("unchecked")
public class CommonUtil {
//在独立的main函数中使用下面的方式进行声明logback对象
private static final Logger log = LoggerFactory.getLogger(CommonUtil.class);
/**
* 判断当前项目是否在jar包中
*
* @return 是/否
*/
public static boolean isRunInJar() {
String resourcePath = "/" + CommonUtil.class.getPackageName().replace(".", "/");
URL url = CommonUtil.class.getResource(resourcePath);
if (url.getProtocol().equals("jar")) {
return true;
}
return false;
}
/**
* 获取文本文件内容兼容jar包与文件系统两种情况
*
* @param path 文件路径
* @return 文件内容
* @throws IOException
*/
public static String txt2String(String path) {
InputStream is = CommonUtil.class.getClassLoader().getResourceAsStream(path);
return IoUtil.read(is, "UTF-8");
}
/**
* 加签
*
* @param map
* @return
*/
public static String Sign(Map<String, Object> map, String signKey) {
if (map == null) {
return null;
}
List<String> keyList = new ArrayList<>(map.keySet());
Collections.sort(keyList);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < keyList.size(); i++) {
String key = keyList.get(i);
Object value = map.get(key);
sb.append(key + "=" + value + "&");
}
String signStr = sb.substring(0, sb.length() - 1) + signKey;
String md5Str = DigestUtils.md5Hex(signStr);
return md5Str;
}
}