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

2025-02-27 04:33:24
推荐回答(2个)
回答1:

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);

回答2:

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