IpUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.dayou.utils;
  2. import javax.servlet.http.HttpServletRequest;
  3. import java.net.*;
  4. import java.text.MessageFormat;
  5. import java.util.Enumeration;
  6. import java.util.Objects;
  7. /**
  8. * 获取IP方法
  9. *
  10. * @author ruoyi
  11. */
  12. public class IpUtils {
  13. public static String getIpAddr(HttpServletRequest request) {
  14. if (request == null) {
  15. return "unknown";
  16. }
  17. String ip = request.getHeader("x-forwarded-for");
  18. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  19. ip = request.getHeader("Proxy-Client-IP");
  20. }
  21. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  22. ip = request.getHeader("X-Forwarded-For");
  23. }
  24. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  25. ip = request.getHeader("WL-Proxy-Client-IP");
  26. }
  27. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  28. ip = request.getHeader("X-Real-IP");
  29. }
  30. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  31. ip = request.getRemoteAddr();
  32. }
  33. if (Objects.isNull(ip) || ip.isEmpty()) {
  34. return "127.0.0.1";
  35. }
  36. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip.split(",")[0];
  37. }
  38. public static boolean internalIp(String ip) {
  39. byte[] addr = textToNumericFormatV4(ip);
  40. if (null != addr) {
  41. return internalIp(addr) || "127.0.0.1".equals(ip);
  42. }
  43. return false;
  44. }
  45. private static boolean internalIp(byte[] addr) {
  46. final byte b0 = addr[0];
  47. final byte b1 = addr[1];
  48. // 10.x.x.x/8
  49. final byte SECTION_1 = 0x0A;
  50. // 172.16.x.x/12
  51. final byte SECTION_2 = (byte) 0xAC;
  52. final byte SECTION_3 = (byte) 0x10;
  53. final byte SECTION_4 = (byte) 0x1F;
  54. // 192.168.x.x/16
  55. final byte SECTION_5 = (byte) 0xC0;
  56. final byte SECTION_6 = (byte) 0xA8;
  57. switch (b0) {
  58. case SECTION_1:
  59. return true;
  60. case SECTION_2:
  61. if (b1 >= SECTION_3 && b1 <= SECTION_4) {
  62. return true;
  63. }
  64. case SECTION_5:
  65. switch (b1) {
  66. case SECTION_6:
  67. return true;
  68. default:
  69. break;
  70. }
  71. default:
  72. return false;
  73. }
  74. }
  75. /**
  76. * 将IPv4地址转换成字节
  77. *
  78. * @param text IPv4地址
  79. * @return byte 字节
  80. */
  81. public static byte[] textToNumericFormatV4(String text) {
  82. if (text.length() == 0) {
  83. return null;
  84. }
  85. byte[] bytes = new byte[4];
  86. String[] elements = text.split("\\.", -1);
  87. try {
  88. long l;
  89. int i;
  90. switch (elements.length) {
  91. case 1:
  92. l = Long.parseLong(elements[0]);
  93. if ((l < 0L) || (l > 4294967295L)) {
  94. return null;
  95. }
  96. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  97. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  98. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  99. bytes[3] = (byte) (int) (l & 0xFF);
  100. break;
  101. case 2:
  102. l = Integer.parseInt(elements[0]);
  103. if ((l < 0L) || (l > 255L)) {
  104. return null;
  105. }
  106. bytes[0] = (byte) (int) (l & 0xFF);
  107. l = Integer.parseInt(elements[1]);
  108. if ((l < 0L) || (l > 16777215L)) {
  109. return null;
  110. }
  111. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  112. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  113. bytes[3] = (byte) (int) (l & 0xFF);
  114. break;
  115. case 3:
  116. for (i = 0; i < 2; ++i) {
  117. l = Integer.parseInt(elements[i]);
  118. if ((l < 0L) || (l > 255L)) {
  119. return null;
  120. }
  121. bytes[i] = (byte) (int) (l & 0xFF);
  122. }
  123. l = Integer.parseInt(elements[2]);
  124. if ((l < 0L) || (l > 65535L)) {
  125. return null;
  126. }
  127. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  128. bytes[3] = (byte) (int) (l & 0xFF);
  129. break;
  130. case 4:
  131. for (i = 0; i < 4; ++i) {
  132. l = Integer.parseInt(elements[i]);
  133. if ((l < 0L) || (l > 255L)) {
  134. return null;
  135. }
  136. bytes[i] = (byte) (int) (l & 0xFF);
  137. }
  138. break;
  139. default:
  140. return null;
  141. }
  142. } catch (NumberFormatException e) {
  143. return null;
  144. }
  145. return bytes;
  146. }
  147. public static String getHostIp() {
  148. try {
  149. Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
  150. while (networks.hasMoreElements()) {
  151. Enumeration<InetAddress> addrs = networks.nextElement().getInetAddresses();
  152. while (addrs.hasMoreElements()) {
  153. InetAddress ip = addrs.nextElement();
  154. System.out.println("ip="+ip.getHostAddress());
  155. System.out.println(MessageFormat.format("{0},{1},{2}",ip instanceof Inet4Address,!ip.isSiteLocalAddress(),!ip.isLoopbackAddress()));
  156. if (ip instanceof Inet4Address && !ip.isLoopbackAddress()) {
  157. // 从网卡获取外网IP
  158. System.out.println("return ip="+ip.getHostAddress());
  159. return ip.getHostAddress();
  160. }
  161. }
  162. }
  163. } catch (SocketException e) {
  164. e.printStackTrace();
  165. }
  166. try {
  167. return InetAddress.getLocalHost().getHostAddress();
  168. } catch (UnknownHostException e) {
  169. }
  170. return "127.0.0.1";
  171. }
  172. public static String getHostName() {
  173. try {
  174. return InetAddress.getLocalHost().getHostName();
  175. } catch (UnknownHostException e) {
  176. }
  177. return "未知";
  178. }
  179. public static void main(String[] args) {
  180. System.out.println(getHostIp());
  181. }
  182. }