0ea8854327d690933b0a178d1c17c364de6abaf8.svn-base 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package cn.com.goldenwater.dcproj.utils;
  2. import sun.net.www.protocol.http.HttpURLConnection;
  3. import javax.servlet.http.HttpServletRequest;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.*;
  8. import java.util.Enumeration;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. /**
  12. * 获取IP方法
  13. *
  14. * @author ruoyi
  15. */
  16. public class IpUtils {
  17. public static String getIpAddr(HttpServletRequest request) {
  18. if (request == null) {
  19. return "unknown";
  20. }
  21. String ip = request.getHeader("x-forwarded-for");
  22. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  23. ip = request.getHeader("Proxy-Client-IP");
  24. }
  25. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  26. ip = request.getHeader("X-Forwarded-For");
  27. }
  28. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  29. ip = request.getHeader("WL-Proxy-Client-IP");
  30. }
  31. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  32. ip = request.getHeader("X-Real-IP");
  33. }
  34. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  35. ip = request.getRemoteAddr();
  36. }
  37. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : EscapeUtil.clean(ip);
  38. }
  39. public static boolean internalIp(String ip) {
  40. byte[] addr = textToNumericFormatV4(ip);
  41. return internalIp(addr) || "127.0.0.1".equals(ip);
  42. }
  43. private static boolean internalIp(byte[] addr) {
  44. if (StringUtils.isNull(addr) || addr.length < 2) {
  45. return true;
  46. }
  47. final byte b0 = addr[0];
  48. final byte b1 = addr[1];
  49. // 10.x.x.x/8
  50. final byte SECTION_1 = 0x0A;
  51. // 172.16.x.x/12
  52. final byte SECTION_2 = (byte) 0xAC;
  53. final byte SECTION_3 = (byte) 0x10;
  54. final byte SECTION_4 = (byte) 0x1F;
  55. // 192.168.x.x/16
  56. final byte SECTION_5 = (byte) 0xC0;
  57. final byte SECTION_6 = (byte) 0xA8;
  58. switch (b0) {
  59. case SECTION_1:
  60. return true;
  61. case SECTION_2:
  62. if (b1 >= SECTION_3 && b1 <= SECTION_4) {
  63. return true;
  64. }
  65. case SECTION_5:
  66. switch (b1) {
  67. case SECTION_6:
  68. return true;
  69. }
  70. default:
  71. return false;
  72. }
  73. }
  74. /**
  75. * 将IPv4地址转换成字节
  76. *
  77. * @param text IPv4地址
  78. * @return byte 字节
  79. */
  80. public static byte[] textToNumericFormatV4(String text) {
  81. if (text.length() == 0) {
  82. return null;
  83. }
  84. byte[] bytes = new byte[4];
  85. String[] elements = text.split("\\.", -1);
  86. try {
  87. long l;
  88. int i;
  89. switch (elements.length) {
  90. case 1:
  91. l = Long.parseLong(elements[0]);
  92. if ((l < 0L) || (l > 4294967295L)) {
  93. return null;
  94. }
  95. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  96. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  97. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  98. bytes[3] = (byte) (int) (l & 0xFF);
  99. break;
  100. case 2:
  101. l = Integer.parseInt(elements[0]);
  102. if ((l < 0L) || (l > 255L)) {
  103. return null;
  104. }
  105. bytes[0] = (byte) (int) (l & 0xFF);
  106. l = Integer.parseInt(elements[1]);
  107. if ((l < 0L) || (l > 16777215L)) {
  108. return null;
  109. }
  110. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  111. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  112. bytes[3] = (byte) (int) (l & 0xFF);
  113. break;
  114. case 3:
  115. for (i = 0; i < 2; ++i) {
  116. l = Integer.parseInt(elements[i]);
  117. if ((l < 0L) || (l > 255L)) {
  118. return null;
  119. }
  120. bytes[i] = (byte) (int) (l & 0xFF);
  121. }
  122. l = Integer.parseInt(elements[2]);
  123. if ((l < 0L) || (l > 65535L)) {
  124. return null;
  125. }
  126. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  127. bytes[3] = (byte) (int) (l & 0xFF);
  128. break;
  129. case 4:
  130. for (i = 0; i < 4; ++i) {
  131. l = Integer.parseInt(elements[i]);
  132. if ((l < 0L) || (l > 255L)) {
  133. return null;
  134. }
  135. bytes[i] = (byte) (int) (l & 0xFF);
  136. }
  137. break;
  138. default:
  139. return null;
  140. }
  141. } catch (NumberFormatException e) {
  142. return null;
  143. }
  144. return bytes;
  145. }
  146. private static final Integer TIME_OUT = 1000;
  147. public static String getLocalHostLANAddress() throws Exception {
  148. String ip = "";
  149. String chinaz = "http://ip.chinaz.com";
  150. StringBuilder inputLine = new StringBuilder();
  151. String read = "";
  152. URL url = null;
  153. HttpURLConnection urlConnection = null;
  154. BufferedReader in = null;
  155. try {
  156. url = new URL(chinaz);
  157. try {
  158. urlConnection = (HttpURLConnection) url.openConnection();
  159. urlConnection.setConnectTimeout(TIME_OUT);
  160. urlConnection.setReadTimeout(TIME_OUT);
  161. in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
  162. } catch (Exception e) {
  163. //如果超时,则返回内网ip
  164. return "";
  165. }
  166. while ((read = in.readLine()) != null) {
  167. inputLine.append(read + "\r\n");
  168. }
  169. //System.out.println(inputLine.toString());
  170. } catch (MalformedURLException e) {
  171. e.printStackTrace();
  172. } catch (IOException e) {
  173. e.printStackTrace();
  174. } finally {
  175. if (in != null) {
  176. try {
  177. in.close();
  178. } catch (IOException e) {
  179. // TODO Auto-generated catch block
  180. e.printStackTrace();
  181. }
  182. }
  183. }
  184. Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
  185. Matcher m = p.matcher(inputLine.toString());
  186. if (m.find()) {
  187. String ipstr = m.group(1);
  188. ip = ipstr;
  189. //System.out.println(ipstr);
  190. }
  191. if ("".equals(ip)) {
  192. // 如果没有外网IP,就返回内网IP
  193. return "";
  194. }
  195. return ip;
  196. }
  197. public static String getHostIp() {
  198. try {
  199. return InetAddress.getLocalHost().getHostAddress();
  200. } catch (UnknownHostException e) {
  201. }
  202. return "127.0.0.1";
  203. }
  204. public static String getHostName() {
  205. try {
  206. return InetAddress.getLocalHost().getHostName();
  207. } catch (UnknownHostException e) {
  208. }
  209. return "未知";
  210. }
  211. }