我本机的局域网地址是192.168.1.100,我现在想用java程序获取自己本机的外网地址,怎么弄啊?有代码最好,没有代码,给点思路也行的。

解决方案 »

  1.   

      思路就是这样的利用ipconfig /all命令
      然后利用流去解析结果
      代码参考
    import   java.io.InputStreamReader;  
      import   java.io.BufferedReader;  
       
      public   class   macaddress   {  
          public   static   void   main(String[]   args)   {  
              macaddress   mdd   =   new   macaddress();  
              String   str=mdd.getMacOnWindow();  
              System.out.println(str);  
          }  
          private   static   String   getMacOnWindow()   {  
                      String   s   =   "";  
                      try   {  
                              String   s1   =   "ipconfig   /all";  
                              Process   process   =   Runtime.getRuntime().exec(s1);  
                              BufferedReader   bufferedreader   =   new   BufferedReader(  
                                              new   InputStreamReader(process.getInputStream()));  
                              String   nextLine;  
                              for   (String   line   =   bufferedreader.readLine();   line   !=   null;   line   =   nextLine)   {  
                                      nextLine   =   bufferedreader.readLine();  
                                      if   (line.indexOf("Physical   Address")   <=   0)   {  
                                              continue;  
                                      }  
                                      int   i   =   line.indexOf("Physical   Address")   +   36;  
                                      s   =   line.substring(i);  
                                      break;  
                              }  
       
                              bufferedreader.close();  
                              process.waitFor();  
                      }   catch   (Exception   exception)   {  
                              s   =   "";  
                      }  
                      return   s.trim();  
              }  
       
      }
      

  2.   

    方式还有很多,
    一,也可以用netstat -an获取命令端口,其中就有你的外网IP地址,然后在去解析结果
    二,还可以调转到http://www.ip138.com/进行查询IP外网地址,然后抓取外网IP地址结果。。
      

  3.   

    一楼的应该只适用于window系统,其它的系统也类似吧
      

  4.   

       在终端下用ipconfig /all得到的是N行N列的字符数组,再自己subString就可以了啊,楼主你先参考1楼的代码试着得到
    所有的字符串,然后再自己subString出对自己有用的字符串
      

  5.   


    package featureTest;import java.net.*;
    import java.io.*;public class IPTest 
    {
      public static void main ( String[] args ) throws IOException 
      {    
        InetAddress[]   inetAdds   =   InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()); 
        for(int i = 0 ; i < inetAdds.length; i++){
         System.out.print(inetAdds[i].getHostName()+ ":\t");
         System.out.println(inetAdds[i].getHostAddress());
        }
      }
    }
    第二个就是dynamic ip
      

  6.   

    简单来说,这本声是一个网络的问题。局域网内的主机想要知道网关的外网IP。
    这个信息你是不可能通过自己主机的命令了解到的。网关也不会告诉你,除非你自己就是网关。需要外网的其他机器告诉你。比如你用浏览器访问www.ip138.com是,其网页会返回告诉你你在外网上真实的IP信息。那么你可以连接www.ip138.com,获取网页内容,截取关键字,得到本机在外网的IP.