2020-04-16 01:53:25 +08:00
|
|
|
|
package com.yutou.tools.utils;
|
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
2021-11-06 16:07:02 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2020-06-03 00:01:11 +08:00
|
|
|
|
import com.yutou.tools.interfaces.DownloadInterface;
|
2020-05-27 14:40:25 +08:00
|
|
|
|
import com.yutou.tools.nas.UpdateIp;
|
2021-10-23 13:03:45 +08:00
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2021-06-25 18:26:19 +08:00
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
|
|
|
|
import org.springframework.core.annotation.AnnotationUtils;
|
2020-10-21 18:05:33 +08:00
|
|
|
|
import org.springframework.core.io.FileSystemResource;
|
2021-06-25 18:26:19 +08:00
|
|
|
|
import org.springframework.core.type.filter.TypeFilter;
|
2020-10-21 18:05:33 +08:00
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2021-10-23 13:03:45 +08:00
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
import org.springframework.security.core.userdetails.User;
|
2021-06-25 18:26:19 +08:00
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
2020-10-27 17:20:31 +08:00
|
|
|
|
import org.springframework.util.StringUtils;
|
2021-06-25 18:26:19 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2020-05-14 17:48:34 +08:00
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2020-04-17 14:32:22 +08:00
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
2020-04-16 01:53:25 +08:00
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2020-04-17 14:32:22 +08:00
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2020-06-03 00:01:11 +08:00
|
|
|
|
import java.io.*;
|
2021-06-25 18:26:19 +08:00
|
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
|
import java.lang.reflect.Method;
|
2020-04-17 14:32:22 +08:00
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
2020-12-04 14:09:12 +08:00
|
|
|
|
import java.net.URLDecoder;
|
2020-04-17 14:32:22 +08:00
|
|
|
|
import java.net.URLEncoder;
|
2020-12-25 10:51:11 +08:00
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-11-27 18:33:21 +08:00
|
|
|
|
import java.security.MessageDigest;
|
2021-01-22 18:18:08 +08:00
|
|
|
|
import java.text.SimpleDateFormat;
|
2021-06-25 18:26:19 +08:00
|
|
|
|
import java.util.*;
|
2020-04-16 01:53:25 +08:00
|
|
|
|
|
|
|
|
|
public class Tools {
|
2021-10-23 13:03:45 +08:00
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
/**
|
2020-05-27 14:40:25 +08:00
|
|
|
|
* 设置Cookie
|
|
|
|
|
*
|
2020-04-17 14:32:22 +08:00
|
|
|
|
* @param response
|
|
|
|
|
* @param key
|
|
|
|
|
* @param value
|
|
|
|
|
* @param time
|
|
|
|
|
*/
|
2020-05-27 14:40:25 +08:00
|
|
|
|
public static void setCookie(HttpServletResponse response, String key, String value, int time) {
|
2020-04-17 14:32:22 +08:00
|
|
|
|
Cookie cookie = new Cookie(key, value);
|
2020-05-27 14:40:25 +08:00
|
|
|
|
if (time != -1) {
|
2020-04-17 14:32:22 +08:00
|
|
|
|
cookie.setMaxAge(time);
|
|
|
|
|
}
|
2020-05-04 03:26:52 +08:00
|
|
|
|
cookie.setPath("/");
|
2020-04-17 14:32:22 +08:00
|
|
|
|
response.addCookie(cookie);
|
2020-05-04 03:26:52 +08:00
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取Cookie
|
2020-05-27 14:40:25 +08:00
|
|
|
|
*
|
2020-04-17 14:32:22 +08:00
|
|
|
|
* @param request
|
|
|
|
|
* @param key
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2020-05-27 14:40:25 +08:00
|
|
|
|
public static Cookie getCookie(HttpServletRequest request, String key) {
|
2020-04-17 14:32:22 +08:00
|
|
|
|
Cookie[] cookies = request.getCookies();
|
2020-05-04 03:26:52 +08:00
|
|
|
|
try {
|
|
|
|
|
for (Cookie cookie : cookies) {
|
2020-05-27 14:40:25 +08:00
|
|
|
|
if (key != null && cookie.getName().equals(key)) {
|
2020-05-04 03:26:52 +08:00
|
|
|
|
return cookie;
|
|
|
|
|
}
|
2020-04-16 01:53:25 +08:00
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
} catch (Exception ignored) {
|
2020-05-04 03:26:52 +08:00
|
|
|
|
|
2020-04-16 01:53:25 +08:00
|
|
|
|
}
|
2020-05-04 03:26:52 +08:00
|
|
|
|
|
2020-04-16 01:53:25 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除Cookie
|
2020-05-27 14:40:25 +08:00
|
|
|
|
*
|
2020-04-17 14:32:22 +08:00
|
|
|
|
* @param request
|
|
|
|
|
* @param response
|
|
|
|
|
* @param key
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) {
|
2020-05-04 03:26:52 +08:00
|
|
|
|
for (Cookie cookie : request.getCookies()) {
|
2020-05-27 14:40:25 +08:00
|
|
|
|
if (cookie.getName().equals(key)) {
|
|
|
|
|
System.out.println("删除key=" + key);
|
2020-05-04 03:26:52 +08:00
|
|
|
|
cookie.setMaxAge(0);
|
|
|
|
|
cookie.setPath("/");
|
|
|
|
|
cookie.setValue(null);
|
|
|
|
|
response.addCookie(cookie);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "ok";
|
2020-04-17 14:32:22 +08:00
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
|
|
|
|
public static void sendServer(String title, String msg) {
|
|
|
|
|
try {
|
|
|
|
|
System.out.println("title=" + title + " msg=" + msg);
|
2020-12-25 10:51:11 +08:00
|
|
|
|
HttpTools.post("https://sctapi.ftqq.com/SCT2619Tpqu93OYtQCrK4LOZYEfr2irm.send",
|
2021-11-06 16:07:02 +08:00
|
|
|
|
("title=" + URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(msg, "UTF-8")).getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
if (!StringUtils.isEmpty(UpdateIp.nas_ip)) {
|
2020-10-27 17:20:31 +08:00
|
|
|
|
String img = null;
|
|
|
|
|
msg = msg.replace("<br/>", "\n");
|
|
|
|
|
if (msg.contains("![logo]")) {
|
|
|
|
|
try {
|
|
|
|
|
img = msg.split("!\\[logo\\]\\(")[1].split("\\)")[0];
|
|
|
|
|
msg = msg.replace("", "");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String url;
|
|
|
|
|
if (img == null) {
|
|
|
|
|
url = "http://" + UpdateIp.nas_ip + ":8000/qq/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8") + "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor";
|
|
|
|
|
} else {
|
|
|
|
|
url = "http://" + UpdateIp.nas_ip + ":8000/qq/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8")
|
|
|
|
|
+ "&imgUrl=" + img
|
|
|
|
|
+ "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor";
|
|
|
|
|
}
|
2020-05-29 10:26:55 +08:00
|
|
|
|
System.out.println(url);
|
2020-12-25 10:51:11 +08:00
|
|
|
|
HttpTools.get(url);
|
2020-05-27 14:40:25 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2020-04-17 14:32:22 +08:00
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取项目路径
|
2020-05-27 14:40:25 +08:00
|
|
|
|
*
|
2020-04-17 14:32:22 +08:00
|
|
|
|
* @param request
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getPath(HttpServletRequest request) {
|
|
|
|
|
return request.getServletContext().getRealPath("/") + "/";
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取客户端IP
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getRemoteAddress(HttpServletRequest request) {
|
|
|
|
|
String ip = request.getHeader("x-forwarded-for");
|
|
|
|
|
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
|
|
|
|
|
ip = request.getHeader("Proxy-Client-IP");
|
|
|
|
|
}
|
|
|
|
|
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
|
|
|
|
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
|
|
}
|
|
|
|
|
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
|
|
|
|
|
ip = request.getRemoteAddr();
|
|
|
|
|
}
|
|
|
|
|
return ip;
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
2020-04-17 14:32:22 +08:00
|
|
|
|
/**
|
|
|
|
|
* N以内的不重复随机数
|
|
|
|
|
*
|
2020-05-27 14:40:25 +08:00
|
|
|
|
* @param min 最小值
|
|
|
|
|
* @param max 最大值
|
2020-04-17 14:32:22 +08:00
|
|
|
|
* @param n
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int[] randomCommon(int min, int max, int n) {
|
|
|
|
|
int len = max - min + 1;
|
|
|
|
|
if (max < min || n > len) {
|
|
|
|
|
return new int[0];
|
|
|
|
|
}
|
|
|
|
|
// 初始化给定范围的待选数组
|
|
|
|
|
int[] source = new int[len];
|
|
|
|
|
for (int i = min; i < min + len; i++) {
|
|
|
|
|
source[i - min] = i;
|
|
|
|
|
}
|
|
|
|
|
int[] result = new int[n];
|
|
|
|
|
Random rd = new Random();
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (int i = 0; i < result.length; i++) {
|
|
|
|
|
// 待选数组0到(len-2)随机一个下标
|
|
|
|
|
index = Math.abs(rd.nextInt() % len--);
|
|
|
|
|
// 将随机到的数放入结果集
|
|
|
|
|
result[i] = source[index];
|
|
|
|
|
// 将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
|
|
|
|
|
source[index] = source[len];
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
|
|
|
|
public static int checkWebLogin(HttpServletRequest request) {
|
|
|
|
|
JSONArray array = new JSONArray();
|
|
|
|
|
if (RedisTools.get("bean") != null) {
|
|
|
|
|
array = JSONArray.parseArray(RedisTools.get("bean"));
|
2020-04-17 14:32:22 +08:00
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
if (array.contains(Tools.getRemoteAddress(request))) {
|
2020-04-17 14:32:22 +08:00
|
|
|
|
System.out.println("IP已被封禁");
|
|
|
|
|
return -100;
|
|
|
|
|
}
|
|
|
|
|
Cookie cookie = Tools.getCookie(request, "user");
|
|
|
|
|
if (cookie == null) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
if (RedisTools.get(cookie.getValue()).equals("ok")) {
|
|
|
|
|
return 1;
|
2020-04-17 14:32:22 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
|
2020-05-14 17:48:34 +08:00
|
|
|
|
/**
|
|
|
|
|
* 保存上传的文件
|
|
|
|
|
*
|
2020-05-27 14:40:25 +08:00
|
|
|
|
* @param path 路径
|
|
|
|
|
* @param file 文件
|
2020-05-14 17:48:34 +08:00
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static String createFile(String path, MultipartFile file, String newFileName) throws Exception {
|
2020-05-27 14:40:25 +08:00
|
|
|
|
String savePath = new File("").getAbsolutePath() + File.separator + "html" + File.separator + path;
|
2020-05-14 17:48:34 +08:00
|
|
|
|
File servicePath = new File(savePath);
|
|
|
|
|
if (!servicePath.exists()) {
|
|
|
|
|
servicePath.mkdirs();
|
|
|
|
|
}
|
2020-05-27 14:40:25 +08:00
|
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
|
|
if (newFileName != null) {
|
|
|
|
|
fileName = newFileName;
|
2020-05-14 17:48:34 +08:00
|
|
|
|
}
|
|
|
|
|
File saveFile = new File(savePath + "/" + fileName);
|
2020-05-27 14:40:25 +08:00
|
|
|
|
if (saveFile.exists()) {
|
|
|
|
|
if (!saveFile.delete()) {
|
|
|
|
|
saveFile = new File(savePath + "/" + fileName.replace(".", "_" + new Date().getTime() + "."));
|
2020-05-14 17:48:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file.transferTo(saveFile);
|
2020-05-27 14:40:25 +08:00
|
|
|
|
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
|
2022-01-28 20:28:32 +08:00
|
|
|
|
return saveFile.getAbsolutePath();
|
2020-05-14 17:48:34 +08:00
|
|
|
|
}
|
2020-06-03 00:01:11 +08:00
|
|
|
|
|
|
|
|
|
public static void download(String url, DownloadInterface downloadInterface) {
|
|
|
|
|
try {
|
2020-10-27 17:20:31 +08:00
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
2020-12-25 10:51:11 +08:00
|
|
|
|
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
|
2020-06-03 00:01:11 +08:00
|
|
|
|
connection.disconnect();
|
|
|
|
|
new File("tmp").mkdirs();
|
2020-10-27 17:20:31 +08:00
|
|
|
|
File file = new File("tmp" + File.separator + url.trim().split("/")[url.trim().split("/").length - 1]);
|
|
|
|
|
if (file.exists()) {
|
2020-06-03 00:01:11 +08:00
|
|
|
|
file.delete();
|
|
|
|
|
}
|
2020-10-27 17:20:31 +08:00
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(file);
|
|
|
|
|
InputStream inputStream = connection.getInputStream();
|
|
|
|
|
byte[] bytes = new byte[4096];
|
2020-06-03 00:01:11 +08:00
|
|
|
|
int len;
|
2020-10-27 17:20:31 +08:00
|
|
|
|
while ((len = inputStream.read(bytes)) != -1) {
|
|
|
|
|
outputStream.write(bytes, 0, len);
|
2020-06-03 00:01:11 +08:00
|
|
|
|
outputStream.flush();
|
|
|
|
|
}
|
|
|
|
|
outputStream.close();
|
|
|
|
|
inputStream.close();
|
|
|
|
|
downloadInterface.onDownload(file.getAbsolutePath());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
downloadInterface.onError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-06 16:07:02 +08:00
|
|
|
|
|
|
|
|
|
public static ResponseEntity<FileSystemResource> getFile(File file, MediaType mediaType) {
|
2021-08-17 07:42:04 +08:00
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
//headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
|
|
try {
|
|
|
|
|
headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
|
|
|
|
}
|
|
|
|
|
//headers.add("Pragma", "no-cache");
|
2021-11-06 16:07:02 +08:00
|
|
|
|
// headers.add("Expires", "0");
|
|
|
|
|
// headers.add("Last-Modified", new Date().toString());
|
|
|
|
|
// headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
2021-08-17 07:42:04 +08:00
|
|
|
|
headers.remove("Vary");
|
|
|
|
|
headers.remove("Connection");
|
|
|
|
|
headers.remove("Content-Disposition");
|
|
|
|
|
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(mediaType).body(new FileSystemResource(file));
|
|
|
|
|
}
|
2021-11-06 16:07:02 +08:00
|
|
|
|
|
2020-10-26 16:50:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 构造给前端的文件
|
2020-10-27 17:20:31 +08:00
|
|
|
|
*
|
2020-10-26 16:50:16 +08:00
|
|
|
|
* @param file 文件路径
|
|
|
|
|
* @return 前端获取的文件
|
|
|
|
|
*/
|
2020-10-27 17:20:31 +08:00
|
|
|
|
public static ResponseEntity<FileSystemResource> getFile(File file) {
|
2021-11-06 16:07:02 +08:00
|
|
|
|
return getFile(file, MediaType.parseMediaType("application/octet-stream"));
|
2020-10-21 18:05:33 +08:00
|
|
|
|
}
|
2020-12-25 10:51:11 +08:00
|
|
|
|
|
2020-11-27 18:33:21 +08:00
|
|
|
|
public static String getFileMD5(File file) {
|
|
|
|
|
if (!file.isFile()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
MessageDigest digest = null;
|
|
|
|
|
FileInputStream in = null;
|
|
|
|
|
byte buffer[] = new byte[1024];
|
|
|
|
|
int len;
|
|
|
|
|
try {
|
|
|
|
|
digest = MessageDigest.getInstance("MD5");
|
|
|
|
|
in = new FileInputStream(file);
|
|
|
|
|
while ((len = in.read(buffer, 0, 1024)) != -1) {
|
|
|
|
|
digest.update(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
in.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return bytesToHexString(digest.digest());
|
|
|
|
|
}
|
2020-12-25 10:51:11 +08:00
|
|
|
|
|
2020-11-27 18:33:21 +08:00
|
|
|
|
private static String bytesToHexString(byte[] src) {
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder("");
|
|
|
|
|
if (src == null || src.length <= 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
for (byte aSrc : src) {
|
|
|
|
|
int v = aSrc & 0xFF;
|
|
|
|
|
String hv = Integer.toHexString(v);
|
|
|
|
|
if (hv.length() < 2) {
|
|
|
|
|
stringBuilder.append(0);
|
|
|
|
|
}
|
|
|
|
|
stringBuilder.append(hv);
|
|
|
|
|
}
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
|
}
|
2020-12-25 10:51:11 +08:00
|
|
|
|
|
|
|
|
|
public static String base64ToString(String base) {
|
|
|
|
|
base = base.replace(" ", "+");
|
2020-12-04 14:09:12 +08:00
|
|
|
|
try {
|
2020-12-25 10:51:11 +08:00
|
|
|
|
base = new String(Base64.getDecoder().decode(base.replace("\r\n", "").getBytes()));
|
|
|
|
|
} catch (Exception e) {
|
2020-12-04 14:09:12 +08:00
|
|
|
|
try {
|
2020-12-25 10:51:11 +08:00
|
|
|
|
base = URLDecoder.decode(base, "UTF-8");
|
|
|
|
|
base = base.replace(" ", "+");
|
|
|
|
|
base = new String(Base64.getDecoder().decode(base.replace("\r\n", "").getBytes()));
|
2020-12-04 14:09:12 +08:00
|
|
|
|
} catch (Exception e1) {
|
|
|
|
|
e1.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return base;
|
2020-11-27 18:33:21 +08:00
|
|
|
|
}
|
2020-12-25 10:51:11 +08:00
|
|
|
|
|
2020-12-02 11:53:13 +08:00
|
|
|
|
/**
|
|
|
|
|
* 异常输出
|
2020-12-25 10:51:11 +08:00
|
|
|
|
*
|
2020-12-02 11:53:13 +08:00
|
|
|
|
* @param e 异常
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getExceptionString(Exception e) {
|
2020-12-25 10:51:11 +08:00
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
|
PrintWriter printWriter = new PrintWriter(writer);
|
2020-12-02 11:53:13 +08:00
|
|
|
|
e.printStackTrace(printWriter);
|
|
|
|
|
printWriter.close();
|
|
|
|
|
return writer.toString();
|
|
|
|
|
}
|
2021-01-22 18:18:08 +08:00
|
|
|
|
|
|
|
|
|
public static String getToDayTime() {
|
|
|
|
|
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
|
|
}
|
2021-06-25 18:26:19 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 扫描使用注解的类
|
2021-11-06 16:07:02 +08:00
|
|
|
|
*
|
2021-06-25 18:26:19 +08:00
|
|
|
|
* @param packageName 扫描包名
|
2021-11-06 16:07:02 +08:00
|
|
|
|
* @param annotation 注解类
|
2021-06-25 18:26:19 +08:00
|
|
|
|
* @return 扫描到的集合
|
|
|
|
|
*/
|
|
|
|
|
public static List<Class> scanClass(String packageName, Class<? extends Annotation> annotation) {
|
|
|
|
|
List<Class> classList = new ArrayList<>();
|
|
|
|
|
if (ObjectUtils.isEmpty(packageName)) {
|
|
|
|
|
return classList;
|
|
|
|
|
}
|
|
|
|
|
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
|
|
|
|
|
TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true;
|
|
|
|
|
provider.addIncludeFilter(includeFilter);
|
|
|
|
|
Set<BeanDefinition> beanDefinitionSet = new HashSet<>();
|
|
|
|
|
// 指定扫描的包名
|
|
|
|
|
Set<BeanDefinition> candidateComponents = provider.findCandidateComponents(packageName);
|
|
|
|
|
beanDefinitionSet.addAll(candidateComponents);
|
|
|
|
|
|
|
|
|
|
beanDefinitionSet.forEach(beanDefinition -> {
|
|
|
|
|
try {
|
|
|
|
|
Class clazz = Class.forName(beanDefinition.getBeanClassName());
|
|
|
|
|
|
|
|
|
|
if (!ObjectUtils.isEmpty(annotation)) {
|
|
|
|
|
if (!ObjectUtils.isEmpty(AnnotationUtils.getAnnotation(clazz, annotation))) {
|
|
|
|
|
classList.add(clazz);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
classList.add(clazz);
|
|
|
|
|
}
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
// System.out.println(definition.getBeanClassName());
|
|
|
|
|
});
|
|
|
|
|
return classList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Url
|
2021-11-06 16:07:02 +08:00
|
|
|
|
*
|
2021-06-25 18:26:19 +08:00
|
|
|
|
* @param packageName 扫描包名
|
2021-11-06 16:07:02 +08:00
|
|
|
|
* @param className 指定类,如无指定类,为null即可
|
2021-06-25 18:26:19 +08:00
|
|
|
|
* @return url集合
|
|
|
|
|
*/
|
2021-11-06 16:07:02 +08:00
|
|
|
|
public static List<String> getUrls(String packageName, String className) {
|
|
|
|
|
List<Class> list = scanClass(packageName, Controller.class);
|
|
|
|
|
List<String> urls = new ArrayList<>();
|
2021-06-25 18:26:19 +08:00
|
|
|
|
for (Class aClass : list) {
|
2021-11-06 16:07:02 +08:00
|
|
|
|
if (className != null && !aClass.getSimpleName().equals(className)) {
|
2021-06-25 18:26:19 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-11-06 16:07:02 +08:00
|
|
|
|
Method[] methods = aClass.getDeclaredMethods();
|
2021-06-25 18:26:19 +08:00
|
|
|
|
for (Method method : methods) {
|
2021-11-06 16:07:02 +08:00
|
|
|
|
RequestMapping ls = method.getAnnotation(RequestMapping.class);
|
|
|
|
|
if (ls != null) {
|
2021-06-25 18:26:19 +08:00
|
|
|
|
urls.add(ls.value()[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return urls;
|
|
|
|
|
}
|
2021-11-06 16:07:02 +08:00
|
|
|
|
|
|
|
|
|
public static String getLoginUser() {
|
|
|
|
|
Object user = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
|
|
if (user instanceof String) {
|
2021-10-23 13:03:45 +08:00
|
|
|
|
return (String) user;
|
2021-11-06 16:07:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
return ((User) user).getUsername();
|
2021-10-23 13:03:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-06 16:07:02 +08:00
|
|
|
|
|
|
|
|
|
public static boolean isAdminLogin() {
|
2021-10-23 13:03:45 +08:00
|
|
|
|
return "admin".equals(getLoginUser());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 16:07:02 +08:00
|
|
|
|
public static String getMD5(String str) {
|
2021-10-23 13:03:45 +08:00
|
|
|
|
return DigestUtils.md5Hex(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getToDayNowTimeToString() {
|
|
|
|
|
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 16:07:02 +08:00
|
|
|
|
public static String getPinYin(String text) {
|
2022-01-20 14:09:11 +08:00
|
|
|
|
String pinyin = HttpTools.get("http://api.tianapi.com/pinyin/index?key=a1e0f7267037c4e0ea02fb1cb3912367&text=" + URLEncoder.encode(text, StandardCharsets.UTF_8));
|
|
|
|
|
JSONObject json = JSONObject.parseObject(pinyin);
|
|
|
|
|
if (json.getInteger("code") == 200) {
|
|
|
|
|
return json.getJSONArray("newslist").getJSONObject(0).getString("pinyin").trim().replace(" ", "");
|
2021-11-06 16:07:02 +08:00
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-16 01:53:25 +08:00
|
|
|
|
}
|