网上那些例子 试了本来都不会 都不知从何下手不用cmdconfig 利用 获取doc 下的那些都写 rec(ipconfig)不用这个
我这win7 和 xp 都不一样谢谢了 苦等interfaceaddress 这个泪下的那个方法呀 

解决方案 »

  1.   

    子网掩码
    http://stackoverflow.com/questions/1221517/how-to-get-subnet-mask-using-java
      

  2.   


    public static void main(String[] args) throws IOException {
    Process pro = Runtime.getRuntime().exec("ipconfig");
    BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
    List<String> rowList = new ArrayList();
    String temp;
    while((temp = br.readLine()) != null){
    rowList.add(temp );
    }
    for (String string : rowList) {
    if(string.indexOf("Subnet Mask") != -1){
    Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
    if(mc.find()){
    System.out.println("子掩码:" + mc.group());
    }else{
    System.out.println("子掩码为空");
    }
    };
    if(string.indexOf("Default Gateway") != -1){
    Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
    if(mc.find()){
    System.out.println("默认网关:" + mc.group());
    }else{
    System.out.println("默认网关为空");
    }
    return;
    };
    }
    }
      

  3.   

    不使用ipconfig调用,感觉真无办法了
    interfaceaddress 这个类方法不多,但只能够获取IP 地址、子网掩码和广播地址,体现不了你需要的默认路由
      

  4.   

       连接特定的 DNS 后缀 . . . . . . . :
       本地链接 IPv6 地址. . . . . . . . : fe80::50ec:6b19:91ee:a2f8%11
       IPv4 地址 . . . . . . . . . . . . : 192.0.0.91
       子网掩码  . . . . . . . . . . . . : 255.255.255.0
       IPv4 地址 . . . . . . . . . . . . : 192.168.0.212
       子网掩码  . . . . . . . . . . . . : 255.255.255.0
       默认网关. . . . . . . . . . . . . : 192.168.0.1隧道适配器 本地连接*:   媒体状态  . . . . . . . . . . . . : 媒体已断开
       连接特定的 DNS 后缀 . . . . . . . :隧道适配器 Teredo Tunneling Pseudo-Interface:   连接特定的 DNS 后缀 . . . . . . . :
       IPv6 地址 . . . . . . . . . . . . : 2001:0:4137:9e76:2cae:5ea:3f57:ff2b
       本地链接 IPv6 地址. . . . . . . . : fe80::2cae:5ea:3f57:ff2b%12
       默认网关. . . . . . . . . . . . . :隧道适配器 6TO4 Adapter:   连接特定的 DNS 后缀 . . . . . . . :
       IPv6 地址 . . . . . . . . . . . . : 2002:c000:5b::c000:5b
       默认网关. . . . . . . . . . . . . : 2002:c058:6301::c058:6301隧道适配器 isatap.{16B95F9C-D611-40D6-8100-61B57E08E7F3}:   媒体状态  . . . . . . . . . . . . : 媒体已断开
       连接特定的 DNS 后缀 . . . . . . . :C:\Users\Administrator>这是win 7 你还截串呀
      

  5.   

    for example
    import java.util.*;
    import java.net.*;
    public class Test {
        public static void main(String[] args) throws Throwable {
            //这里作为测试直接取IP地址,LZ可以根据需要自己调整调用相应的方法
            InetAddress ip = InetAddress.getByAddress(new byte[]{127, 0, 0, 1}); 
            NetworkInterface ni = NetworkInterface.getByInetAddress(ip);
            List<InterfaceAddress> list = ni.getInterfaceAddresses();
            if (list.size() > 0) {
                int mask = list.get(0).getNetworkPrefixLength(); //子网掩码的二进制1的个数
                StringBuilder maskStr = new StringBuilder();
                int[] maskIp = new int[4];
                for (int i=0; i<maskIp.length; i++) {
                    maskIp[i] = (mask >= 8) ? 255 : (mask > 0 ? (mask & 0xff) : 0);
                    mask -= 8;
                    maskStr.append(maskIp[i]);
                    if (i < maskIp.length-1) {maskStr.append(".");}
                }
                System.out.println(maskStr);
            }
        }
    }
      

  6.   


    public static void main(String[] args) throws IOException {
    String os = System.getProperties().getProperty("os.name"); //得到操作系统 xp 为"Windows XP"  其他的的楼主自己去试试
    Process pro = Runtime.getRuntime().exec("ipconfig");
    BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
    List<String> rowList = new ArrayList();
    String temp;
    while((temp = br.readLine()) != null){
    rowList.add(temp );
    }
    for (String string : rowList) {
    String sm=  os.equals("Windows XP") ? "Subnet Mask" : "子网掩码" ; //这里只判断了win7个xp 
    if(string.indexOf("Subnet Mask") != -1){
    Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
    if(mc.find()){
    System.out.println("子掩码:" + mc.group());
    }else{
    System.out.println("子掩码为空");
    }
    };
    String dg =  os.equals("Windows XP") ? "Default Gateway" : "默认网关" ; //这里只判断了win7个xp 
    if(string.indexOf("Default Gateway") != -1){
    Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
    if(mc.find()){
    System.out.println("默认网关:" + mc.group());
    }else{
    System.out.println("默认网关为空");
    }
    return;
    };
    }
    }
      

  7.   


    public static void main(String[] args) throws IOException {
    String os = System.getProperties().getProperty("os.name"); //得到操作系统 xp 为"Windows XP"  其他的的楼主自己去试试
    Process pro = Runtime.getRuntime().exec("ipconfig");
    BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
    List<String> rowList = new ArrayList();
    String temp;
    while((temp = br.readLine()) != null){
    rowList.add(temp );
    }
    for (String string : rowList) {
    String sm=  os.equals("Windows XP") ? "Subnet Mask" : "子网掩码" ; //这里只判断了win7个xp 
    if(string.indexOf(sm) != -1){
    Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
    if(mc.find()){
    System.out.println("子掩码:" + mc.group());
    }else{
    System.out.println("子掩码为空");
    }
    };
    String dg =  os.equals("Windows XP") ? "Default Gateway" : "默认网关" ; //这里只判断了win7个xp 
    if(string.indexOf(dg) != -1){
    Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
    if(mc.find()){
    System.out.println("默认网关:" + mc.group());
    }else{
    System.out.println("默认网关为空");
    }
    return;
    };
    }
    }修正一下。
      

  8.   

    额,不对啊,再试试看
    不过还是调用系统命令ipconfig取比较靠谱
    import java.util.*;
    import java.net.*;
    public class Test {
        public static void main(String[] args) throws Throwable {
            InetAddress ip = InetAddress.getByAddress(new byte[]{127,0,0,1});
            NetworkInterface ni = NetworkInterface.getByInetAddress(ip);
            List<InterfaceAddress> list = ni.getInterfaceAddresses();
            if (list.size() > 0) {
                int mask = list.get(0).getNetworkPrefixLength();
                System.out.println(mask); //这里打印一下mask看看是什么值?
                mask = (-1 >> (31-(mask-1))) << (31-(mask-1));
                StringBuilder maskStr = new StringBuilder();
                byte[] maskIp = new byte[4];
                for (int i=0; i<maskIp.length; i++) {
                    maskIp[i] = (byte)(mask>>(maskIp.length-1-i)*8);
                    maskStr.append((maskIp[i] & 0xff));
                    if (i < maskIp.length-1) {maskStr.append(".");}
                }
                System.out.println(maskStr);
            }
        }
    }
      

  9.   

    第一时间想到的是这个:InetAddress
    顶阿宝哥
      

  10.   

    128
    255.255.255.255  阿宝哥 你说咋改  ( //这里作为测试直接取IP地址,LZ可以根据需要自己调整调用相应的方法
    )还有那个 byte 我写 192 想换127就是不行呢  192就报错是啥个道理
      

  11.   

    使用java.lang.Process类啊
    将"ipconfig /all"传进去
    /**
     * 获取linuxShell命令结果.
     * @param cmd 指令
     * @return 指令结果
     */
    public String getRuntimeExec(final String cmd) {
    BufferedReader bufferedReader = null;
    Process process = null;
    StringBuffer sb = new StringBuffer();
    try {
    String buffer;
    //System.out.println(cmd);
    process = Runtime.getRuntime().exec(cmd);
    // 得到结果
    bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while ((buffer = bufferedReader.readLine()) != null) {
    sb.append(buffer + "\n");
    }
    if (sb.length() > 0) {
    sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("execute failed.");
    return null;
    } finally {
    // 清理
    try {
    if (bufferedReader != null) {
    bufferedReader.close();
    bufferedReader = null;
    } if (process != null) {
    process.destroy();
    process = null;
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  12.   

    byte的范围是-128到127,所以大于这个范围的要用强行转换
    InetAddress ip = InetAddress.getByAddress(new byte[]{(byte)192,168,0,1}); //for exampe
      

  13.   

    还有啊,你是ipv4还是ipv6?
    查看一下javadoc
    public class InterfaceAddressextends Object此类表示网络接口地址。简言之,对于 IPv4 地址,是指 IP 地址、子网掩码和广播地址。对于 IPv6 地址,是指 IP 地址和网络前缀长度。 
    import java.util.*;
    import java.net.*;
    public class Test {
        public static void main(String[] args) throws Throwable {
            InetAddress ip = InetAddress.getByAddress(new byte[]{127,0,0,1});
            NetworkInterface ni = NetworkInterface.getByInetAddress(ip);
            List<InterfaceAddress> list = ni.getInterfaceAddresses();
            for(int i=0; i<list.size(); i++) { //用for循环试试看是否取得ipv4或ipv6
                int mask = list.get(i).getNetworkPrefixLength();
                System.out.println(mask); //这里打印一下mask看看是什么值?
                mask = (-1 >> (31-(mask-1))) << (31-(mask-1));
                StringBuilder maskStr = new StringBuilder();
                byte[] maskIp = new byte[4];
                for (int i=0; i<maskIp.length; i++) {
                    maskIp[i] = (byte)(mask>>(maskIp.length-1-i)*8);
                    maskStr.append((maskIp[i] & 0xff));
                    if (i < maskIp.length-1) {maskStr.append(".");}
                }
                System.out.println(maskStr);
            }
        }
    }
      

  14.   

    InterfaceAddresss只能在JDK1.6以上的环境下使用
      

  15.   

    也来弄一段:,只能得到掩码public static void main(String args[]) {
    try {
    for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); 
    nis != null && nis.hasMoreElements();) {
    NetworkInterface ni = nis.nextElement(); System.out.println("网络适配器:" + ni.getDisplayName());
    for (InterfaceAddress ifAddr : ni.getInterfaceAddresses()) {
    System.out.println("IP:" + ifAddr.getAddress().getHostAddress());
    System.out.println("Mask:" + getMask(ifAddr.getNetworkPrefixLength()) );
    //System.out.println("Borad:"+ ifAddr.getBroadcast().getHostAddress());
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * 返回指定长度的掩码的字符串,如,长度为8的掩码,返回255.0.0.0;长度为24的掩码,返回255.255.255.0<br/>
     * 注:只适用于IPV4
     * @param maskLength 掩码长度
     * @return
     */
    private static String getMask(int maskLength){ StringBuffer maskStr = new StringBuffer();
    int mask = 0xFFFFFFFF << 32 - maskLength ;
    for(int i = 3 ;i >= 0;i--){
    maskStr.append( (  mask  >> (8*i) ) & 0xFF);
    if(i>0){
    maskStr.append(".");
    }
    }
    return maskStr.toString();
    }
      

  16.   

    ipv4  宝哥 你哪个在我这 获得是255.0.0.0
      

  17.   

    你要获取哪台机器的ip和掩码
    InetAddress ip = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});//这个只是作为例子,要你指定实际的ip地址
    如果你是获取本机的,直接用getLocalHost()  
    即改成
    InetAddress ip = InetAddress.getLocalHost();