import javaxservlet>
《java》中提取本地IP的方法如下:
private static void getIpAddressByNetworkInterface() {
try {
Enumeration<NetworkInterface> nets = NetworkInterfacegetNetworkInterfaces();
NetworkInterface net;
InetAddress inetAddress;
while (netshasMoreElements()) {
net = netsnextElement();
Enumeration<InetAddress> address = netgetInetAddresses();
while (addresshasMoreElements()) {
inetAddress = addressnextElement();
if (inetAddress!=null&&inetAddress instanceof Inet4Address)
Systemoutprintln(inetAddressgetHostAddress());
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
import javanetInetAddress;import javautilEnumeration;
import javanetNetworkInterface;
import javautil;
public class ipdisplay {
/
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String allipaddress;
ArrayList ar = new ArrayList();
Enumeration netInterfaces = NetworkInterfacegetNetworkInterfaces();
while (netInterfaceshasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfacesnextElement();
Enumeration cardipaddress = nigetInetAddresses();
InetAddress ip = (InetAddress) cardipaddressnextElement();
if(!ipgetHostAddress()equalsIgnoreCase("127001") )
{ aradd(nigetName()+":");
allipaddress=ipgetHostAddress();
while(cardipaddresshasMoreElements())
{
ip = (InetAddress) cardipaddressnextElement();
allipaddress=allipaddress+" , "+ipgetHostAddress();
}
aradd(allipaddress);
}
else
continue;
}
for(int i=0;i<arsize();)
{
Systemoutprintln(arget(i++));
}
}
}java获取本机的外网ip示例:
import javaioIOException;
import javaioInputStream;
import javanet>java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
Systemoutprintln(MaingetRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
Enumeration<NetworkInterface> netInterfaces =
NetworkInterfacegetNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaceshasMoreElements() && !finded) {
NetworkInterface ni = netInterfacesnextElement();
Enumeration<InetAddress> address = nigetInetAddresses();
while (addresshasMoreElements()) {
ip = addressnextElement();
if (!ipisSiteLocalAddress()
&& !ipisLoopbackAddress()
&& ipgetHostAddress()indexOf(":") == -1) {// 外网IP
netip = ipgetHostAddress();
finded = true;
break;
} else if (ipisSiteLocalAddress()
&& !ipisLoopbackAddress()
&& ipgetHostAddress()indexOf(":") == -1) {// 内网IP
localip = ipgetHostAddress();
}
}
}
if (netip != null && !""equals(netip)) {
return netip;
} else {
return localip;
}
}
}
Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:
import javanet;public class NetInfo {
public static void main(String[] args) {
new NetInfo()say();
}
public void say() {
try {
InetAddress i = InetAddressgetLocalHost();
Systemoutprintln(i); //计算机名称和IP
Systemoutprintln(igetHostName()); //名称
Systemoutprintln(igetHostAddress()); //只获得IP
}
catch(Exception e){eprintStackTrace();}
}
}
也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。
是否可以解决您的问题?方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmdexe /c ipconfig | findstr IPv4";
try {
Process p = RuntimegetRuntime()exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(pgetInputStream()));
String line = null;
while((line = brreadLine()) != null){
line = linesubstring(linelastIndexOf(":")+2,linelength());
sbappend(line);
}
brclose();
pdestroy();
} catch (IOException e) {
eprintStackTrace();
}
return sbtoString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterfacegetNetworkInterfaces();
while (enhasMoreElements()) {
NetworkInterface intf = (NetworkInterface) ennextElement();
Enumeration<InetAddress> enumIpAddr = intfgetInetAddresses();
while (enumIpAddrhasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddrnextElement();
if (!inetAddressisLoopbackAddress() && !inetAddressisLinkLocalAddress()
&& inetAddressisSiteLocalAddress()) {
sbappend(inetAddressgetHostAddress()toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sbtoString();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)