发布网友 发布时间:2022-04-23 01:12
共5个回答
好二三四 时间:2022-06-10 17:16
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
很多朋友都想知道java如何获取本地ip?下面就一起来了解一下吧~
获取java本地ip一共有两种方法:1、inetAddress类;2、封装方法。
1、 inetAddress类
通过InetAddress的实例对象包含以数字形式保存的IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。InetAddress类提供了将主机名解析为IP地址(或反之)的方法。其生成InetAddress对象的方法。
import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; public class Main { public static void main(String[] args) throws UnknownHostException { //Inet4Address address= (Inet4Address) Inet4Address.getLocalHost(); InetAddress address = InetAddress.getLocalHost(); System.out.println(address);//获取计算机名称和ip地址 String hostAddress = address.getHostAddress(); System.out.println(hostAddress);//获取ip地址 String hostName = address.getHostName(); System.out.println(hostName);//获取计算机名称 } }
2、封装方法。
public static String getLocalIp() { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface nif = netInterfaces.nextElement(); Enumeration<InetAddress> InetAddress = nif.getInetAddresses(); while (InetAddress.hasMoreElements()) { String ip = InetAddress.nextElement().getHostAddress(); if (ip.startsWith("192.168")) { return ip; } } } } catch (SocketException e) { } return "127.0.0.1"; }
以上就是小编今天的分享了,希望可以帮到大家。
热心网友 时间:2022-06-10 14:41
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();
}
}
}
热心网友 时间:2022-06-10 16:16
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;//获得本机IP
address=addr.getHostName()toString;//获得本机名称
热心网友 时间:2022-06-10 18:07
貌似这个很爽
import java.net.InetAddress;
import java.net.UnknownHostException;
class Demo {
public static void main(String[] args) throws UnknownHostException {
InetAddress ip = InetAddress.getLocalHost();
System.out.println("本机的IP : " + ip.getHostAddress());
System.out.println("本机的计算机名 :" + ip.getHostName());
}
}
===============下面这个也可以==================
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
class Demo {
public static void main(String[] args) throws IOException {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface)allNetInterfaces.nextElement();
// System.out.println(netInterface.getName());
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
}
}
==============
偶是菜,纯粹复制转载的,抛异常导下包,弄个泛型,注释了一行而已____异常貌似抛错了.求指教,是否需要try catch环绕
=================
试了一下环绕
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
class Demo {
public static void main(String[] args) {
Enumeration<NetworkInterface> allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface
.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
// System.out.println(netInterface.getName());
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
}
}
继续求指教
热心网友 时间:2022-06-10 20:15
就是刚才那高手回答的 能给你解决问题