CommonUtil.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.template.common.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import javax.servlet.http.HttpServletRequest;
  4. import java.net.InetAddress;
  5. import java.net.UnknownHostException;
  6. import java.security.MessageDigest;
  7. import java.time.LocalDate;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. import java.util.*;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. /**
  14. * 公共工具类
  15. **/
  16. @Slf4j
  17. public class CommonUtil {
  18. /**
  19. * 检查字符串是否符合正则表达式
  20. */
  21. public static boolean checkStrByRegx(String regex, String str) {
  22. //使用Pattern类的compile()方法编译正则表达式
  23. Pattern pattern = Pattern.compile(regex);
  24. //使用Matcher类的matcher()方法将正则表达式和字符串进行匹配
  25. Matcher matcher = pattern.matcher(str);
  26. //返回匹配结果
  27. return matcher.matches();
  28. }
  29. /**
  30. * 检测客户端参数
  31. *
  32. * @param code 状态码
  33. * @param object 对象
  34. * @return 返回类型
  35. */
  36. public static Map<String, Object> getReturnMap(String code, Object object) {
  37. Map<String, Object> returnMap = new HashMap<>();
  38. returnMap.put("code", code);
  39. returnMap.put("msg", object);
  40. return returnMap;
  41. }
  42. public static String getNumberFromString(String string) {
  43. Pattern pattern = Pattern.compile("\\d+");
  44. Matcher matcher = pattern.matcher(string);
  45. if (matcher.find()) {
  46. return matcher.group();
  47. }
  48. return null;
  49. }
  50. /**
  51. * 获取ip
  52. *
  53. * @param request
  54. * @return
  55. */
  56. public static String getIpAddr(HttpServletRequest request) {
  57. String ipAddress = null;
  58. try {
  59. ipAddress = request.getHeader("x-forwarded-for");
  60. System.out.println("x-forwarded-for=" + ipAddress);
  61. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  62. ipAddress = request.getHeader("Proxy-Client-IP");
  63. System.out.println("Proxy-Client-IP=" + ipAddress);
  64. }
  65. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  66. ipAddress = request.getHeader("WL-Proxy-Client-IP");
  67. System.out.println("WL-Proxy-Client-IP=" + ipAddress);
  68. }
  69. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  70. ipAddress = request.getRemoteAddr();
  71. System.out.println("getRemoteAddr=" + ipAddress);
  72. if (ipAddress.equals("127.0.0.1")) {
  73. // 根据网卡取本机配置的IP
  74. InetAddress inet = null;
  75. try {
  76. inet = InetAddress.getLocalHost();
  77. } catch (UnknownHostException e) {
  78. e.printStackTrace();
  79. }
  80. ipAddress = inet.getHostAddress();
  81. System.out.println("getHostAddress=" + ipAddress);
  82. }
  83. }
  84. // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
  85. if (ipAddress != null && ipAddress.length() > 15) {
  86. // "***.***.***.***".length()
  87. // = 15
  88. if (ipAddress.indexOf(",") > 0) {
  89. ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
  90. }
  91. }
  92. } catch (Exception e) {
  93. ipAddress = "";
  94. }
  95. return ipAddress;
  96. }
  97. /**
  98. * 获取全部请求头
  99. *
  100. * @param request
  101. * @return
  102. */
  103. public static Map<String, String> getAllRequestHeader(HttpServletRequest request) {
  104. Enumeration<String> headerNames = request.getHeaderNames();
  105. Map<String, String> map = new HashMap<>();
  106. while (headerNames.hasMoreElements()) {
  107. String key = headerNames.nextElement();
  108. //根据名称获取请求头的值
  109. String value = request.getHeader(key);
  110. map.put(key, value);
  111. }
  112. return map;
  113. }
  114. /**
  115. * MD5加密
  116. *
  117. * @param data
  118. * @return
  119. */
  120. public static String MD5(String data) {
  121. try {
  122. MessageDigest md = MessageDigest.getInstance("MD5");
  123. byte[] array = md.digest(data.getBytes("UTF-8"));
  124. StringBuilder sb = new StringBuilder();
  125. for (byte item : array) {
  126. sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
  127. }
  128. return sb.toString().toUpperCase();
  129. } catch (Exception exception) {
  130. }
  131. return null;
  132. }
  133. /**
  134. * 获取验证码随机数
  135. *
  136. * @param length
  137. * @return
  138. */
  139. public static String getRandomCode(int length) {
  140. String sources = "0123456789";
  141. Random random = new Random();
  142. StringBuilder sb = new StringBuilder();
  143. for (int j = 0; j < length; j++) {
  144. sb.append(sources.charAt(random.nextInt(9)));
  145. }
  146. return sb.toString();
  147. }
  148. /**
  149. * 获取当前时间戳
  150. *
  151. * @return
  152. */
  153. public static long getCurrentTimestamp() {
  154. return System.currentTimeMillis();
  155. }
  156. /**
  157. * 生成uuid
  158. *
  159. * @return
  160. */
  161. public static String generateUUID() {
  162. return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
  163. }
  164. /**
  165. * 获取随机长度的串
  166. *
  167. * @param length
  168. * @return
  169. */
  170. private static final String ALL_CHAR_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  171. public static String getStringNumRandom(int length) {
  172. //生成随机数字和字母,
  173. Random random = new Random();
  174. StringBuilder saltString = new StringBuilder(length);
  175. for (int i = 1; i <= length; ++i) {
  176. saltString.append(ALL_CHAR_NUM.charAt(random.nextInt(ALL_CHAR_NUM.length())));
  177. }
  178. return saltString.toString();
  179. }
  180. /**
  181. * LocalDate 拼接时间转成 LocalDateTime
  182. *
  183. * @param localDate
  184. * @param timeStr HH:mm:ss
  185. * @return
  186. */
  187. public static LocalDateTime localDateToTime(LocalDate localDate, String timeStr) {
  188. LocalTime time = LocalTime.parse(timeStr);
  189. return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(),
  190. time.getHour(), time.getMinute(), time.getSecond());
  191. }
  192. }