首页 > 代码库 > 关于IP操作的工具类

关于IP操作的工具类

  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操作的工具类