博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java获取登陆用户的IP地址
阅读量:7028 次
发布时间:2019-06-28

本文共 2634 字,大约阅读时间需要 8 分钟。

/** * 通过HttpServletRequest返回IP地址 * @param request HttpServletRequest * @return ip String * @throws Exception */public String getIpAddr(HttpServletRequest request) throws Exception {    String ip = request.getHeader("x-forwarded-for");    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        ip = request.getHeader("Proxy-Client-IP");    }    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        ip = request.getHeader("WL-Proxy-Client-IP");    }    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        ip = request.getHeader("HTTP_CLIENT_IP");    }    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        ip = request.getHeader("HTTP_X_FORWARDED_FOR");    }    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        ip = request.getRemoteAddr();    }    return ip;} /** * 通过IP地址获取MAC地址 * @param ip String,127.0.0.1格式 * @return mac String * @throws Exception */public String getMACAddress(String ip) throws Exception {    String line = "";    String macAddress = "";    final String MAC_ADDRESS_PREFIX = "MAC Address = ";    final String LOOPBACK_ADDRESS = "127.0.0.1";    //如果为127.0.0.1,则获取本地MAC地址。    if (LOOPBACK_ADDRESS.equals(ip)) {        InetAddress inetAddress = InetAddress.getLocalHost();        //貌似此方法需要JDK1.6。        byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();        //下面代码是把mac地址拼装成String        StringBuilder sb = new StringBuilder();        for (int i = 0; i < mac.length; i++) {            if (i != 0) {                sb.append("-");            }            //mac[i] & 0xFF 是为了把byte转化为正整数            String s = Integer.toHexString(mac[i] & 0xFF);            sb.append(s.length() == 1 ? 0 + s : s);        }        //把字符串所有小写字母改为大写成为正规的mac地址并返回        macAddress = sb.toString().trim().toUpperCase();        return macAddress;    }    //获取非本地IP的MAC地址    try {        Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);        InputStreamReader isr = new InputStreamReader(p.getInputStream());        BufferedReader br = new BufferedReader(isr);        while ((line = br.readLine()) != null) {            if (line != null) {                int index = line.indexOf(MAC_ADDRESS_PREFIX);                if (index != -1) {                    macAddress = line.substring(index + MAC_ADDRESS_PREFIX.length()).trim().toUpperCase();                }            }        }        br.close();    } catch (IOException e) {        e.printStackTrace(System.out);    }    return macAddress;}

代码来自网络,支持转载~·

转载于:https://www.cnblogs.com/nomengfei/p/5949209.html

你可能感兴趣的文章
会计电算化模拟试题9
查看>>
一名大学生在银行工作8年的职场感悟
查看>>
阻带窗函数[数字信号处理]使用窗函数设计FIR滤波器
查看>>
客户端生成nginx webdav配置
查看>>
接外包私活成功之道(一)-注重服务意识,挖掘深层需求
查看>>
GSM-串口和GPRS-网口通信
查看>>
技术人生:向前端人员学习
查看>>
【产品经理】产品经理的十大顶级错误
查看>>
“AIR SDK 0.0: AIR SDK location “...\devsdks\AIRSDK\Win” does not exist.”问题解决~
查看>>
识别Andriod APK签名证书类型
查看>>
获取CentOS软件源中的updates包
查看>>
git使用说明
查看>>
HTML5 Canvas实现黑客帝国文字掉落效果
查看>>
web 缓存
查看>>
【cocos2d-x 手游研发----怪物智能AI】
查看>>
值得拥有!精心推荐几款超实用的 CSS 开发工具
查看>>
NumberUtils、ArrayUtils和RandomUtils工具类用法
查看>>
转:MAVEN常用命令
查看>>
<三>年编程经验、何去何从?
查看>>
MVC应用程序,动态创建单选列表(RadioButtonList)
查看>>