java代码 获取到本机的真实网卡IP与所有的IPV4IP地址

7 篇文章 0 订阅
4 篇文章 0 订阅

前言

之前我们的项目中需要对项目中的CAS认证方式做异地登录验证,所以需要在客户端登录的时候将自己的IP地址传送过去,给服务器进行校验。
这个时候我们就需要获取通过java代码获取到本机的真实ip。在网上找到了很多关于这方面的资料,发现他们的方式只能获取到192.168.*.*这个 环回地址,还有一些方式获取到的是本机中所有网卡的ip,包括 虚拟机网卡,蓝牙虚拟网卡,这个和与访问外网的网卡比起来,只差一个过滤。
我这里将网络上面的这些方式进行了一些总结,得到了一个可以获取到真实网卡IP的方法

代码展示


package com.supermap.digicty.sdm;

import java.io.UnsupportedEncodingException;
import java.net.*;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @Author: zhangjun
 * @Description:
 * @Date: Create in 12:37 2020/5/11 
 */
public class testLocalIP {
    /**
     * 获取本机本地IP
     */
     static void getLocalIP(){
        InetAddress ia=null;
        try {
            ia=ia.getLocalHost();
            String localname=ia.getHostName();
            String localip=ia.getHostAddress();
            System.out.println("本机名称是:"+ localname);
            System.out.println("本机的ip是 :"+localip);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    /**
     * 获取到所有的在活动的网卡IP 包含虚拟网卡
     * @return
     */
     static List<String> getLocalIPList() {
        List<String> ipList = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface;
            Enumeration<InetAddress> inetAddresses;
            InetAddress inetAddress;
            String ip;
            while (networkInterfaces.hasMoreElements()) {
                networkInterface = networkInterfaces.nextElement();
                inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    inetAddress = inetAddresses.nextElement();
                    if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
                        System.out.println(inetAddress.getHostName());
                        ip = inetAddress.getHostAddress();
                        System.out.println(ip);
                        ipList.add(ip);
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ipList;
    }
     public static void main(String[] args) {
         System.out.println(getRealIP());

     }
    /**
     * 获取本地真正的IP地址,即获得有线或者无线WiFi地址(真实物理网卡IP)。
     * 过滤虚拟机、蓝牙等地址
     */
    public static String getRealIP() {
        try {
            //获取到所有的网卡
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface =  allNetInterfaces.nextElement();
                // 去除回环接口127.0.0.1,子接口,未运行的接口
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }
                //获取名称中是否包含 Intel Realtek 的网卡
                if (!netInterface.getDisplayName().contains("Intel")
                        && !netInterface.getDisplayName().contains("Realtek")
                        && !netInterface.getDisplayName().contains("Atheros")
                        && !netInterface.getDisplayName().contains("Broadcom")) {
                    continue;
                }
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                System.out.println(netInterface.getDisplayName());
                while (addresses.hasMoreElements()) {
                    InetAddress ip = addresses.nextElement();
                    if (ip != null) {
                        if (ip instanceof Inet4Address) {
                            System.out.println(ip.getHostName());
                            return ip.getHostAddress();
                        }
                    }
                }
                break;
            }
        } catch (SocketException e) { e.getMessage(); }
        return null;
    }
}

运行效果

在这里插入图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值