您的位置:首页 > 编程语言 > Java开发

利用Java获取IP地址(整理)

2012-03-09 12:33 351 查看
package ipDemo;

import java.io.File;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class GetIpAddress {
public static void main(String[] args) {
if(args.length==0){
printLocalInfo();
}else{
printInputInfo(args);
}
}
/**
*获取本地IP
*/
static void printLocalInfo(){
try {
Enumeration<NetworkInterface> interfaces
= NetworkInterface.getNetworkInterfaces();
if(interfaces == null){
System.out.println("There isn't network interface!");
}else{
while(interfaces.hasMoreElements()){
NetworkInterface interInfo = interfaces.nextElement();
System.out.println("Interface:"+interInfo.getName()+"--");
Enumeration<InetAddress> addressEnum = interInfo.getInetAddresses();
if(!addressEnum.hasMoreElements()){
System.out.println("\t(No address for this interface)");
}
while(addressEnum.hasMoreElements()){
InetAddress address = addressEnum.nextElement();
String str =null;
if(address instanceof Inet4Address){
str = "(IPv4)";
}else if(address instanceof Inet6Address){
str = "(IPv6)";
}else{
str = "(?)";
}
System.out.print("\tAddress "+str);
System.out.println(File.separator+address.getHostAddress());
}
}
}
} catch (SocketException se) {
System.out.println("NetWork interface error:"+se.getMessage());
}
}
/**
*获取输入信息的IP
*/
static void printInputInfo(String[] args){
for(String host:args){
try {
System.out.println(host+":");
InetAddress[] addressArray = InetAddress.getAllByName(host);
for(InetAddress address:addressArray){
System.out.println("\t"+address.getHostName()+File.separator
+address.getHostAddress());
}
} catch (UnknownHostException e) {
System.out.println("\tUnable to find address for "+host);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: