java获得IP地址
来源:百度知道 编辑:UC知道 时间:2024/05/29 08:55:01
我用的是宽带拨号上网,在dos窗口中用ipconfig命令输出如下:
C:\Documents and Settings\ASUS>ipconfig
Windows IP Configuration
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 10.1.41.101
Subnet Mask . . . . . . . . . . . : 255.0.0.0
Default Gateway . . . . . . . . . : 10.1.41.1
Ethernet adapter 无线网络连接:
Media State . . . . . . . . . . . : Media disconnected
Ethernet adapter SoftEther Virtual LAN Connection:
Media State . . . . . . . . . . . : Media disconnected
PPP adapter 宽带连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 121.229.6.189
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 121.229.6.189
用一般的java方法输出的是本地连接的ip:10.1.41.101,而我需要的是宽带连接的ip:12
下面有一篇文章,介绍若何读取物理网卡的地址 ,同样的
你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE5.0新特性之ProcessBuilder
这个例子使用了J2SE5.0的ProcessBuilder类执行外部的程序,相对于 Runtime.exec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package com.kuaff.jdk5package;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ProcessBuilderShow
{
public static List getPhysicalAddress()
{
Process p = null;
//物理网卡列表
List address = new ArrayList();
try
{
//执行ipconfig /all命令
p = new ProcessBuilder("ipconfig", "/all").start();
}
catch (IOException e)
{
return address;
}
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
//读取进程输出值
InputStream in = p.getInpu