「java本机ip」java本机ip包捕获和解析
本篇文章给大家谈谈java本机ip,以及java本机ip包捕获和解析对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
如何用 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();
}
}
}
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地址
下面有一篇文章,介绍若何读取物理网卡的地址 ,同样的
你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE5.0新特性之ProcessBuilder
这个例子使用了J2SE5.0的ProcessBuilder类执行外部的程序,相对于 Runtime.exec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package com.kuaff.jdk5package;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ProcessBuilderShow
{
public static List getPhysicalAddress()
{
Process p = null;
//物理网卡列表
List address = new ArrayList();
try
{
//执行ipconfig /all命令
p = new ProcessBuilder("ipconfig", "/all").start();
}
catch (IOException e)
{
return address;
}
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
//读取进程输出值
InputStream in = p.getInputStream();
try
{
while (in.read(b)0)
{
sb.append(new String(b));
}
}
catch (IOException e1)
{
}
finally
{
try
{
in.close();
}
catch (IOException e2)
{
}
}
//以下分析输出值,得到物理网卡
String rtValue = sb.substring(0);
int i = rtValue.indexOf("Physical Address. . . . . . . . . :");
while(i0)
{
rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());
address.add(rtValue.substring(0,18));
i = rtValue.indexOf("Physical Address. . . . . . . . . :");
}
return address;
}
public static void main(String[] args)
{
List address = ProcessBuilderShow.getPhysicalAddress();
for(String add:address)
{
System.out.printf("物理网卡地址:%s%n", add);
}
}
}
java如何获取当前登录ip
第一种:获取本机的IP
EnumerationNetworkInterface
netInterfaces
=
null;
try
{
netInterfaces
=
NetworkInterface.getNetworkInterfaces();
while
(netInterfaces.hasMoreElements())
{
NetworkInterface
ni
=
netInterfaces.nextElement();
System.out.println("DisplayName:"
+
ni.getDisplayName());
System.out.println("Name:"
+
ni.getName());
EnumerationInetAddress
ips
=
ni.getInetAddresses();
while
(ips.hasMoreElements())
{
System.out.println("IP:"
+
ips.nextElement().getHostAddress());
ipTemp=
ni.getInetAddresses().nextElement().getHostAddress();
if(ipTemp!="127.0.0.1"
!"127.0.0.1".equals(ipTemp))
{
ip=ipTemp;
}
}
}
}catch(Exception
ee)
{
ee.printStackTrace();
}
第二种:也是本机的:
InetAddress
addr
=
InetAddress.getLocalHost();
ip=addr.getHostAddress().toString();//获得本机IP
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();
}
}
}
java本机ip的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java本机ip包捕获和解析、java本机ip的信息别忘了在本站进行查找喔。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。