1. 程式人生 > >關於IP操作的工具類

關於IP操作的工具類

*** equal rac 獲取本地ip pan -i str upper forward

  1 public class IPUtil {
  2     /**
  3      * 獲取本地IP地址
  4      * @return
  5      */
  6     public static String getLocalIP() {
  7         String LocalIp = "";
  8         try {
  9             InetAddress[] mArLocalIP = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
 10             LocalIp = null
!= mArLocalIP && mArLocalIP.length > 0 ? mArLocalIP[0].getHostAddress() : LocalIp; 11 } catch (Exception e) { 12 e.printStackTrace(); 13 } 14 return LocalIp; 15 } 16 17 /** 18 * 獲取客戶端的地址 19 * @param request 20 * @return
21 */ 22 public static String getIpAddr(HttpServletRequest request) { 23 String ipAddr = null; 24 ipAddr = request.getHeader("x-forwarded-for"); 25 if (ipAddr == null || ipAddr.length() == 0 || "unknown".equalsIgnoreCase(ipAddr)) { 26 ipAddr = request.getHeader("Proxy-Client-IP");
27 } 28 if (ipAddr == null || ipAddr.length() == 0 || "unknown".equalsIgnoreCase(ipAddr)) { 29 ipAddr = request.getHeader("WL-Proxy-Client-IP"); 30 } 31 if (ipAddr == null || ipAddr.length() == 0 || "unknown".equalsIgnoreCase(ipAddr)) { 32 ipAddr = request.getRemoteAddr(); 33 if ("127.0.0.1".equals(ipAddr) || "0:0:0:0:0:0:0:1".equals(ipAddr)) { 34 //根據網卡取本機配置的IP 35 try { 36 InetAddress inet = InetAddress.getLocalHost(); 37 ipAddr = inet.getHostAddress(); 38 } catch (UnknownHostException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 44 //對於通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照‘,‘分割 45 if (ipAddr != null && ipAddr.length() > 15) {//ip地址中最多12位數字+3個點,如:***.***.***.*** 46 if (ipAddr.indexOf(",") > 0) { 47 ipAddr = ipAddr.substring(0, ipAddr.indexOf(",")); 48 } 49 } 50 return ipAddr; 51 } 52 53 /** 54 * 通過IP地址獲取請求用戶的MAC地址 - 針對windows 7操作系統 55 * @param ip 56 * @return 57 */ 58 public static String getMACAddressOfWin7(String ip){ 59 String macAddress = ""; 60 try { 61 //獲得網絡接口對象(即網卡),並得到mac地址,mac地址存在於一個byte數組中。 62 InetAddress inetAddress = InetAddress.getByName(ip); 63 byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress(); 64 65 //把mac地址拼裝成String 66 StringBuffer sb = new StringBuffer(); 67 for (int i = 0; i < mac.length; i++) { 68 String s = Integer.toHexString(mac[i] & 0xFF); // mac[i] & 0xFF 是為了把byte轉化為正整數 69 sb.append(i != 0 ? "-" : "").append(s.length() == 1 ? 0 + s : s); 70 } 71 macAddress = sb.toString().toUpperCase(); 72 } catch (UnknownHostException e) { 73 e.printStackTrace(); 74 } catch (SocketException e) { 75 e.printStackTrace(); 76 } 77 return macAddress; 78 } 79 80 /** 81 * 通過IP地址獲取請求用戶的MAC地址 - 針對其他版本的windows操作系統 82 * @param ip 83 * @return 84 */ 85 public static String getMACAddressOfWindows(String ip){ 86 String macAddress = ""; 87 try { 88 Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); 89 InputStreamReader isr = new InputStreamReader(p.getInputStream()); 90 BufferedReader br = new BufferedReader(isr); 91 String line = null; 92 while ((line = br.readLine()) != null) { 93 if (null != line && line.trim().length() != 0) { 94 if(line.indexOf("MAC Address") > -1){ 95 macAddress = line.substring(line.indexOf("MAC Address") + 14).trim(); 96 break; 97 }else if(line.indexOf("MAC 地址") > -1){ 98 macAddress = line.substring(line.indexOf("MAC 地址") + 9).trim(); 99 break; 100 } 101 } 102 } 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 return macAddress; 107 } 108 109 /** 110 * 通過IP地址獲取請求用戶的MAC地址 - 針對linux操作系統 111 * @param ip 112 * @return 113 */ 114 public static String getMACAddressOfLinux(String ip){ 115 String macAddress = ""; 116 try { 117 //linux下的命令,一般取eth0作為本地主網卡 118 Process p = Runtime.getRuntime().exec("ifconfig eth0"); 119 120 // 顯示信息中包含有mac地址信息 121 InputStreamReader isr = new InputStreamReader(p.getInputStream()); 122 BufferedReader br = new BufferedReader(isr); 123 String line = null; 124 int index = -1; 125 while ((line = br.readLine()) != null) { 126 index = line.toLowerCase().indexOf("hwaddr"); 127 if (index > -1) { 128 macAddress = line.substring(index + 7).trim(); 129 break; 130 } 131 } 132 } catch (IOException e) { 133 e.printStackTrace(); 134 } 135 return macAddress; 136 } 137 138 /** 139 * 通過IP判斷這臺機子網絡是否正常連接 140 * @param ip 141 * @return 142 */ 143 public static boolean isNetReachable(String ip){ 144 boolean b = false; 145 try { 146 InetAddress inetAddress = InetAddress.getByName(ip); 147 b = inetAddress.isReachable(5*1000); 148 } catch (UnknownHostException e) { 149 e.printStackTrace(); 150 }catch (IOException e) { 151 e.printStackTrace(); 152 } 153 return b; 154 } 155 }

關於IP操作的工具類