发布网友
共5个回答
热心网友
下面有一篇文章,介绍若何读取物理网卡的地址 ,同样的
你可以用这个方法读取你所需要的本机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(i>0)
{
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);
}
}
}
热心网友
简单实现代码如下:
js获取来源页地址方法:
var url = document.referrer;
document.write(url);
jsp获取来源页地址方法:
String url = request.getHeader(”Referer”);
System.out.println(url);
对比两个方法:
1.js里是”referrer”,jsp里是”referer”,前者比后者多一个”r”;
2.前者如直接输入网址,则显示为空,后者显示null;
import java.net.*;
public class ip5 {
public static void main(String args[]) throws Exception {
String ip = InetAddress.getLocalHost().getHostAddress();
System.out.println(ip);
}
}
热心网友
ipconfig -all 试试
热心网友
我晕,
直接InetAddress.getLocalHost()不就够了,
不懂的去看API,不要提这样的问题了,浪费分啊
热心网友
request里面有这个方法,直接获取系统当前IP