|
|
package com.dsideal.gw.Util;
|
|
|
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.JarURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.*;
|
|
|
import java.util.jar.JarEntry;
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
@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");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取所有sql文件,并返回一个list集合
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public static List<String> getAllSql() throws IOException {
|
|
|
List<String> list = new ArrayList<>();
|
|
|
if (isRunInJar()) {
|
|
|
URL url = CommonUtil.class.getClassLoader().getResource("Sql/");
|
|
|
String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2);
|
|
|
URL jarURL = new URL(jarPath);
|
|
|
JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection();
|
|
|
JarFile jarFile = jarCon.getJarFile();
|
|
|
Enumeration<JarEntry> jarEntrys = jarFile.entries();
|
|
|
while (jarEntrys.hasMoreElements()) {
|
|
|
JarEntry entry = jarEntrys.nextElement();
|
|
|
String name = entry.getName();
|
|
|
if (name.startsWith("Sql/") && !entry.isDirectory()) {
|
|
|
list.add(name);
|
|
|
}
|
|
|
}
|
|
|
} else {//如果运行在文件系统中,直接加载sql文件
|
|
|
//遍历sql目录下所有的sql文件
|
|
|
File sqlDir;
|
|
|
String basePath = PathKit.getRootClassPath();
|
|
|
sqlDir = new File(basePath + "/Sql");
|
|
|
File[] sqlFiles = sqlDir.listFiles();
|
|
|
for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) {
|
|
|
if (sqlFile.getName().indexOf(".sql") > 0) {//只加载.sql文件
|
|
|
list.add("Sql/"+sqlFile.getName());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加签
|
|
|
*
|
|
|
* @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;
|
|
|
}
|
|
|
}
|