「java获取本机ip」java获取ip地址的指令

博主:adminadmin 2023-03-19 12:46:07 408

今天给各位分享java获取本机ip的知识,其中也会对java获取ip地址的指令进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何查询本机ip地址和mac地址

   Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

import java.net.*;

public class NetInfo {

 public static void main(String[] args) {

    new NetInfo().say();

    }

 public void say() {

   try {

   InetAddress i = InetAddress.getLocalHost();

   System.out.println(i);                  //计算机名称和IP

   System.out.println(i.getHostName());    //名称

   System.out.println(i.getHostAddress()); //只获得IP

   }

   catch(Exception e){e.printStackTrace();}

 }

}

    也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。

如何用 Java 获取系统 IP

import java.net.*;

public class Test6 {

public static void main(String[] args) {

// TODO Auto-generated method stub

InetAddress ia=null;

try {

ia=ia.getLocalHost();

String localname=ia.getHostName();

String localip=ia.getHostAddress();

System.out.println("本机名称是:"+ localname);

System.out.println("本机的ip是 :"+localip);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

boot项目配置文件中的数据库地址怎样根据本机自动获取

1.通过Java获取本机ip,可以使用InetAddress类的getLocalHost()方法。

2.通过Spring Boot配置文件(比如application.yml)来设置数据库连接,可以使用Spring Boot placeholder实现,如:${local.ip}:3306

3.在SpringBoot应用程序启动时,程序可以在启动参数里指定local.ip属性,获取本机ip,并与端口号组合成数据库链接地址。

java中获取本地IP地址

方法如下:

方法一,使用CMD命令:

public static String getLocalIPForCMD(){

StringBuilder sb = new StringBuilder();

String command = "cmd.exe /c ipconfig | findstr IPv4";

try {

Process p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = null;

while((line = br.readLine()) != null){

line = line.substring(line.lastIndexOf(":")+2,line.length());

sb.append(line);

}

br.close();

p.destroy();

} catch (IOException e) {

e.printStackTrace();

}

return sb.toString();

}

方法二,使用Java方法:

public static String getLocalIPForJava(){

StringBuilder sb = new StringBuilder();

try {

EnumerationNetworkInterface en = NetworkInterface.getNetworkInterfaces();

while (en.hasMoreElements()) {

NetworkInterface intf = (NetworkInterface) en.nextElement();

EnumerationInetAddress enumIpAddr = intf.getInetAddresses();

while (enumIpAddr.hasMoreElements()) {

InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();

if (!inetAddress.isLoopbackAddress() !inetAddress.isLinkLocalAddress()

inetAddress.isSiteLocalAddress()) {

sb.append(inetAddress.getHostAddress().toString()+"\n");

}

}

}

} catch (SocketException e) { }

return sb.toString();

}

关于java获取本机ip和java获取ip地址的指令的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。