import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* description 获取本地Ip地址
*
* @author yanzy
* @date 2019/8/23 17:13
*/
public class Cmd {
public static void main(String[] args) throws Exception {
List<String> result = runCommand("ipconfig");
for (String value : result)
System.out.println(value);
}
public static List<String> runCommand(String command) throws IOException, InterruptedException {
Runtime run = Runtime.getRuntime();
Process proc;
proc = run.exec(command);
InputStream is = proc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
ArrayList response = new ArrayList();
String line;
while ((line = reader.readLine()) != null) {
response.add(line);
}
proc.waitFor();
is.close();
reader.close();
proc.destroy();
return response;
}
}
Java通过cmd获取本地Ip
最新推荐文章于 2022-02-05 15:30:29 发布