CommonUtil.java 5.7 KB

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