Java利用利用API得知当前的DefaultGatway,就是打开IP设置页第三个“默认网关”。

来源:百度知道 编辑:UC知道 时间:2024/05/12 03:21:04
RT,找了好久没找到。
哪个类里有没有类似getDefaultGateway()这样的方法呢?
有Sample Code吗?谢啦~

String command = "cmd.exe /c ipconfig";
Process p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line;
String address = "";
while ((line = br.readLine()) != null) {

if (line.indexOf("Default Gateway") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();

System.out.println(address);

我可以跟你说下我的思路要是还不懂得话可以百度HI我
利用Runtime.getRuntime().exec("ipconfig");执行的结果是个字符串,
再利用正则表达式,抽取 这一行你需要的内容
Default Gateway . . . . . . . . . : 192.168.0.1
明白吗?