java獲取本地ip
阿新 • • 發佈:2019-02-13
package com.tianjian.property.jieshuns.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.JsonObject;
import com.tianjian.property.jieshuns.service.QueryParkSpaceService;
import com.tianjian.property.jieshuns.service.QueryParkSpaceServices;
public class JisshunTest2 {
/* public static void main(String args[]) throws Exception {
String ip = InetAddress.getLocalHost().getHostAddress(); //獲取本地域名
System.out.println(ip);
} */
public void DNSAndIPAddress(){
}
//根據域名獲取ip地址
public static InetAddress getIP(String dns) throws UnknownHostException{
return InetAddress.getByName(dns);
}
//根據ip地址獲取域名
public static InetAddress getDNS(String ip) throws UnknownHostException{
return InetAddress.getByName(ip);
}
//獲取本機的域名和ip地址
public static String getHostAddress() throws UnknownHostException{
return InetAddress.getLocalHost().toString();
}
public static void main(String[] args) throws UnknownHostException{
System.out.println(getIP("www.upc.edu.cn").toString());
System.out.println(getDNS("www.baidu.com").toString());
System.out.println(getDNS("www.csdn.net").toString());
System.out.println(getDNS("www.cnblogs.com").toString());
System.out.println(getDNS("www.importnew.com").toString());
System.out.println(getDNS("zhirublog.lingw.net").toString());
System.out.println(getDNS("wechat.tagen-pm.com").toString());
//System.out.println(getDNS("blog.csdn.net/zhiruchen").toString());
System.out.println(getHostAddress());
System.out.println(getDNS("192.168.1.224").toString());
System.out.println(getDNS("39.108.181.33").toString());
//使用域名建立物件
InetAddress inet1 = InetAddress.getByName("wechat.tagen-pm.com");
System.out.println("---inet1------"+inet1);
//使用IP建立物件
InetAddress inet2 = InetAddress.getByName("39.108.181.33");
System.out.println("---inet2------"+inet2);
//獲得本機地址物件
InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3);
//獲得物件中儲存的域名
String host = inet3.getHostName();
System.out.println("域名:" + host);
//獲得物件中儲存的IP
String ip = inet3.getHostAddress();
System.out.println("IP:" + ip);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.JsonObject;
import com.tianjian.property.jieshuns.service.QueryParkSpaceService;
import com.tianjian.property.jieshuns.service.QueryParkSpaceServices;
public class JisshunTest2 {
/* public static void main(String args[]) throws Exception {
String ip = InetAddress.getLocalHost().getHostAddress(); //獲取本地域名
System.out.println(ip);
} */
public void DNSAndIPAddress(){
}
//根據域名獲取ip地址
public static InetAddress getIP(String dns) throws UnknownHostException{
return InetAddress.getByName(dns);
}
//根據ip地址獲取域名
public static InetAddress getDNS(String ip) throws UnknownHostException{
return InetAddress.getByName(ip);
}
//獲取本機的域名和ip地址
public static String getHostAddress() throws UnknownHostException{
return InetAddress.getLocalHost().toString();
}
public static void main(String[] args) throws UnknownHostException{
System.out.println(getIP("www.upc.edu.cn").toString());
System.out.println(getDNS("www.baidu.com").toString());
System.out.println(getDNS("www.csdn.net").toString());
System.out.println(getDNS("www.cnblogs.com").toString());
System.out.println(getDNS("www.importnew.com").toString());
System.out.println(getDNS("zhirublog.lingw.net").toString());
System.out.println(getDNS("wechat.tagen-pm.com").toString());
//System.out.println(getDNS("blog.csdn.net/zhiruchen").toString());
System.out.println(getHostAddress());
System.out.println(getDNS("192.168.1.224").toString());
System.out.println(getDNS("39.108.181.33").toString());
//使用域名建立物件
InetAddress inet1 = InetAddress.getByName("wechat.tagen-pm.com");
System.out.println("---inet1------"+inet1);
//使用IP建立物件
InetAddress inet2 = InetAddress.getByName("39.108.181.33");
System.out.println("---inet2------"+inet2);
//獲得本機地址物件
InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3);
//獲得物件中儲存的域名
String host = inet3.getHostName();
System.out.println("域名:" + host);
//獲得物件中儲存的IP
String ip = inet3.getHostAddress();
System.out.println("IP:" + ip);
}
}