关于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 }
时间: 2024-08-04 18:46:46

关于IP操作的工具类的相关文章

c语言中字符串操作的工具类

 1.编写头文件 #define _CRT_SECURE_NO_WARNINGS //#pragmawarning(disable:4996) #include <stdio.h> #include <stdlib.h> #include <string.h> struct CString { char *p;        //保存字符串首地址 int reallength; //实际长度 }; typedef struct CString mystring;//

poi操作Excel工具类

在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完成的功能是:读取Excel.写入Excel.合并Excel的功能.

List操作的工具类

1 /** 2 * <p>list操作的工具类</p> 3 */ 4 public class ListUtil { 5 /** 6 * 过滤掉list里面才重复项 7 * 8 * @param list 9 * @return List 10 */ 11 public static List<String> filterRepeat(List<String> list){ 12 int length = list.size(); 13 for(int i

Java 借助poi操作Wold工具类

? Apache封装的POI组件对Excel,Wold的操作已经非常的丰富了,在项目上也会经常用到一些POI的基本操作 这里就简单的阐述POI操作Wold的基本工具类,代码还是有点粗造的,但是不影响使用. 这个类包含了一些对文本进行换行,加粗,倾斜,字体颜色,大小,首行缩进,添加边框等方法.分享给大家学习下: Apache POI的组件: ApachePOI包含用于处理MS-Office的所有OLE2复合文档的类和方法.该API的组件列表如下 - POIFS(不良混淆实现文件系统) - 此组件是

android操作ini工具类

package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Inp

操作集合工具类:Collections

Collections是常用的操作Set.List.Map的工具类.提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合对象设置为不可变.对集合对象实现同步控制等方法. reverse 反转: /** * Reverses the order of the elements in the specified list.<p> * * This method runs in linear time. * * @param list the list whose elements a

自己封装的poi操作Excel工具类

该工具类主要完成的功能是:读取Excel.汇总Excel的功能.在读取时,可以设定开始和结束读取的位置.设定是否读取多个sheet.设定读取那个或者那些sheet等.在汇总时,如设定是否覆盖目标文件.设定是否比较检查重复内容.设定检查重复的列索引等功能. package com.tgb.ccl.excel.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; impo

java与javascript对cookie操作的工具类

Java对cookie的操作 package cn.utils; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 操作cookie的工具类(默认cookie的有效路径为"/")

(4)文本挖掘(一)——准备文本读写及对Map操作的工具类

文本挖掘是一个对具有丰富语义的文本进行分析,从而理解其所包含的内容和意义的过程.文本挖掘包含分词.文本表示.文本特征选择.文本分类.文本聚类.文档自动摘要等方面的内容.文本挖掘的具体流程图可下图所示: 我的项目是以复旦大学中文语料库和路透社英文语料库为数据集的,都是有类别的两层目录文本集. 不管你要做什么,你首先都要先读取文本,为了方便后面的操作,我写了几个工具类,这里先将文本读取Reader类.文本写入Writer类和对Map的各种操作MapUtil类. Reader import java.