導航:首頁 > 編程語言 > java查詢ip

java查詢ip

發布時間:2023-03-18 18:36:15

❶ 如何用 java 獲取系統 IP

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(); } }
是否可以解決您的問題?

閱讀全文

與java查詢ip相關的資料

熱點內容
c盤系統文件佔多少內存 瀏覽:562
今日說法微信聯系方式 瀏覽:238
ai文件導出pdf字體無法嵌入 瀏覽:777
ps保存後會有個文件在桌面 瀏覽:677
sf文件夾 瀏覽:47
怎麼查看qq悄悄話好友 瀏覽:13
excel表格保存後怎麼恢復數據 瀏覽:372
wps文件怎麼改只讀文件 瀏覽:396
java自動補全 瀏覽:54
怎麼提高網路問卷通過率 瀏覽:656
數控火焰切割半圓弧怎麼編程 瀏覽:501
一加六手機數據備份在哪裡 瀏覽:264
手機app脫殼之後怎麼解壓 瀏覽:823
7700用win10 瀏覽:72
搖搖棒c程序 瀏覽:837
文件名後綴為svg是什麼意思 瀏覽:446
免費推廣的網站有哪些 瀏覽:271
微信綁定卡但是忘記密碼怎麼辦 瀏覽:372
資源文件mfc 瀏覽:519
哪裡能接到數據標注 瀏覽:254

友情鏈接