
正文
java代码获取本机ip java获取当前主机ip
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何利用JAVA获得本机实际IP
import a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mHwhPyfvuAD1Phu-PjRk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1c3nW6vPWDz" target="_blank" class="baidu-highlight"java.net/a.*;
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("本机名称是java代码获取本机ip:"+ localname);
System.out.println("本机java代码获取本机ip的ip是 java代码获取本机ip:"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关问答
Q1: java如何获取当前登录ip
为适应不同java代码获取本机ip的网络情况java代码获取本机ip,提供这个类java代码获取本机ip:看下面的代码吧,Copy过去就能用。
import javax.servlet.http.HttpServletRequest;
public class HttpUtil {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
Q2: 如何用 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();
}
}
}
Q3: 怎样用JAVA程序获取本机ip ?
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress inet = InetAddress.getLocalHost();
System.out.println("本机的ip=" + inet.getHostAddress());
}
}
Q4: java中获取本地IP地址
方法如下java代码获取本机ip:
方法一java代码获取本机ip,使用CMD命令java代码获取本机ip:
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代码获取本机ip,使用Java方法java代码获取本机ip:
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();
}
Q5: 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和java获取当前主机ip的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








