
代码亲测可用:
import javanetInetAddress;
import javanetUnknownHostException;
public class TestInetAddress {
InetAddress myIpAddress = null;
InetAddress[] myServer = null;
public static void main(String args[]) {
TestInetAddress address = new TestInetAddress();
Systemoutprintln("Your host IP is: " + addressgetLocalhostIP());
String domain = ",服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的>
为适应不同的网络情况,提供这个类:看下面的代码吧,Copy过去就能用。
import javaxservlet>
1直接用jdk方法
InetAddress address = InetAddressgetLocalHost();
String IP=addressgetHostAddress();
2用request获取
public static String getRemoteIpAddr() {
>
你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE50新特性之ProcessBuilder
这个例子使用了J2SE50的ProcessBuilder类执行外部的程序,相对于 Runtimeexec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package comkuaffjdk5package;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
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 = pgetInputStream();
try
{
while (inread(b)>0)
{
sbappend(new String(b));
}
}
catch (IOException e1)
{
}
finally
{
try
{
inclose();
}
catch (IOException e2)
{
}
}
//以下分析输出值,得到物理网卡
String rtValue = sbsubstring(0);
int i = rtValueindexOf("Physical Address :");
while(i>0)
{
rtValue = rtValuesubstring(i + "Physical Address :"length());
addressadd(rtValuesubstring(0,18));
i = rtValueindexOf("Physical Address :");
}
return address;
}
public static void main(String[] args)
{
List address = ProcessBuilderShowgetPhysicalAddress();
for(String add:address)
{
Systemoutprintf("物理网卡地址:%s%n", add);
}
}
}
public void PingAll() throws Exception{
//首先得到本机的IP,得到网段
InetAddress host = InetAddressgetLocalHost();
String hostAddress = hostgetHostAddress();
int k=0;
k=hostAddresslastIndexOf("");
String ss = hostAddresssubstring(0,k+1);
for(int i=1;i <=255;i++){ //对所有局域网Ip
String iip=ss+i;
Ping(iip);
}
import javanet;
public class Test {
public static void main(String[] args) throws Exception {
String ip = InetAddressgetLocalHost()getHostAddress();
Systemoutprintln(ip);
}
}
以上就是关于java通过域名怎么获取本机ip全部的内容,包括:java通过域名怎么获取本机ip、java编程如何获取本地ip并显示在文本框中、java如何获取当前登录ip等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)