importjava.net.*;
publicclassTest6{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
InetAddressia=null;
try{
ia=ia.getLocalHost();
Stringlocalname=ia.getHostName();
Stringlocalip=ia.getHostAddress();
System.out.println("本機名稱是:"+localname);
System.out.println("本機的ip是:"+localip);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
❷ JAVA怎麼獲取IP地址
java代碼獲取ip地址方法是
調用java.net包下面的的InetAddress類獲取。
❸ java中獲取本地IP地址
方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmd.exe /c ipconfig | findstr IPv4";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
line = line.substring(line.lastIndexOf(":")+2,line.length());
sb.append(line);
}
br.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
sb.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sb.toString();
}
❹ java怎麼獲取請求的ip
java獲取外網ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果沒有配置外網IP則返回它
String netip = null;// 外網IP
Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外網IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 外網IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 內網IP
localip = ip.getHostAddress();
}
}
}
if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}
❺ 如何用java獲取本地ip地址
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* @author Becolette
* @description TODO
* @date 2015-11-5 下午01:58:46
*/
public class IpAddress {
public static String find() {
List<String> ips = new ArrayList<String>();
// 返回所有網路介面的一個枚舉實例
Enumeration<?> allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
// 獲得當前網路介面
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {
ips.add(ip.getHostAddress());
}
}
}
if (ips.size() == 1) {
return ips.get(0);
} else {
for (String ipa : ips) {
if (!"127.0.0.1".equals(ipa)) {
return ipa;
}
}
}
return MacAddress.find();
}
}
❻ java 怎麼查看區域網的ip有哪些
點擊系統桌面左下角的「開始」,然後在搜索框內輸入「cmd」。
註:如果是XP系統,請依次點擊「開始」再點擊「運行」,接著在窗口中輸入「cmd」就可以了,其它步驟相同!
在彈出的CMD命令中輸入:ipconfig /all,並回車。
3
之後,我們會得到如下圖的信息。
這里,我們只需要查看IPv4 地址,默認網關和DNS伺服器就可以了。
如圖標記的一樣,IPv4 地址為我們本機的IP地址,剩餘的默認網管和DNS伺服器也是我們本機的默認網管和DNS地址。
❼ java IP查詢方法
Java編程查詢IP地址歸屬地,可以調用淘寶提供的查詢,並且解析http請求返回的json串,代碼如下:
packagegetAddressByIp;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importnet.sf.json.JSONObject;
publicclassGetAddressByIp
{
/**
*
*@paramIP
*@return
*/
(StringIP){
Stringresout="";
try{
Stringstr=getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);
System.out.println(str);
JSONObjectobj=JSONObject.fromObject(str);
JSONObjectobj2=(JSONObject)obj.get("data");
Stringcode=(String)obj.get("code");
if(code.equals("0")){
resout=obj2.get("country")+"--"+obj2.get("area")+"--"+obj2.get("city")+"--"+obj2.get("isp");
}else{
resout="IP地址有誤";
}
}catch(Exceptione){
e.printStackTrace();
resout="獲取IP地址異常:"+e.getMessage();
}
returnresout;
}
(StringurlStr)
{
try
{//獲取HttpURLConnection連接對象
URLurl=newURL(urlStr);
HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
//設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
//獲取相應碼
intrespCode=httpConn.getResponseCode();
if(respCode==200)
{
returnConvertStream2Json(httpConn.getInputStream());
}
}
catch(MalformedURLExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
return"";
}
(InputStreaminputStream)
{
StringjsonStr="";
//ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStreamout=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
//將輸入流轉移到內存輸出流中
try
{
while((len=inputStream.read(buffer,0,buffer.length))!=-1)
{
out.write(buffer,0,len);
}
//將內存流轉換為字元串
jsonStr=newString(out.toByteArray());
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnjsonStr;
}
}
❽ JAVA獲取IP地址
public static void main(String[] args) { try { // 獲取計算機名 String name = InetAddress.getLocalHost().getHostName(); // 獲取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("計算機名:"+name); System.out.println("IP地址:"+ip); } catch (UnknownHostException e) { System.out.println("異常:" + e); e.printStackTrace(); } }
是否可以解決您的問題?