java代碼查詢ip地址 java如何獲取ip地址

如何獲取局域網(wǎng)內(nèi)所有IP地址 java代碼

1.得到局域網(wǎng)網(wǎng)段,可由自己機器的IP來確定 (也可以手動獲取主機IP-CMD-ipconfig /all)

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、網(wǎng)站建設(shè)、單縣網(wǎng)絡(luò)推廣、重慶小程序開發(fā)公司、單縣網(wǎng)絡(luò)營銷、單縣企業(yè)策劃、單縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供單縣建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:bm7419.com

2.根據(jù)IP類型,一次遍歷局域網(wǎng)內(nèi)IP地址

JAVA類,編譯之后直接運行便可以得到局域網(wǎng)內(nèi)所有IP,具體怎樣使用你自己編寫相應(yīng)代碼調(diào)用便可

代碼如下::

package bean;

import java.io.*;

import java.util.*;

public class Ip{

static public HashMap ping; //ping 后的結(jié)果集

public HashMap getPing(){ //用來得到ping后的結(jié)果集

return ping;

}

//當(dāng)前線程的數(shù)量, 防止過多線程摧毀電腦

static int threadCount = 0;

public Ip() {

ping = new HashMap();

}

public void Ping(String ip) throws Exception{

//最多30個線程

while(threadCount30)

Thread.sleep(50);

threadCount +=1;

PingIp p = new PingIp(ip);

p.start();

}

public void PingAll() throws Exception{

//首先得到本機的IP,得到網(wǎng)段

InetAddress host = InetAddress.getLocalHost();

String hostAddress = host.getHostAddress();

int k=0;

k=hostAddress.lastIndexOf(".");

String ss = hostAddress.substring(0,k+1);

for(int i=1;i =255;i++){ //對所有局域網(wǎng)Ip

String iip=ss+i;

Ping(iip);

}

//等著所有Ping結(jié)束

while(threadCount0)

Thread.sleep(50);

}

public static void main(String[] args) throws Exception{

Ip ip= new Ip();

ip.PingAll();

java.util.Set entries = ping.entrySet();

Iterator iter=entries.iterator();

String k;

while(iter.hasNext()){

Map.Entry entry=(Map.Entry)iter.next();

String key=(String)entry.getKey();

String value=(String)entry.getValue();

if(value.equals("true"))

System.out.println(key+"--"+value);

}

}

class PingIp extends Thread{

public String ip; // IP

public PingIp(String ip){

this.ip=ip;

}

public void run(){

try{

Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");

InputStreamReader ir = new InputStreamReader(p.getInputStream());

LineNumberReader input = new LineNumberReader (ir);

//讀取結(jié)果行

for (int i=1 ; i 7; i++)

input.readLine();

String line= input.readLine();

if (line.length() 17 || line.substring(8,17).equals("timed out"))

ping.put(ip,"false");

else

ping.put(ip,"true");

//線程結(jié)束

threadCount -= 1;

}catch (IOException e){}

}

}

}

java IP查詢方法

Java編程查詢IP地址歸屬地,可以調(diào)用淘寶提供的service查詢,并且解析http請求返回的json串,代碼如下:

package?getAddressByIp;

import?java.io.ByteArrayOutputStream;

import?java.io.IOException;

import?java.io.InputStream;

import?java點虐 .HttpURLConnection;

import?java點虐 .MalformedURLException;

import?java點虐 .URL;

import?net.sf.json.JSONObject;

public?class?GetAddressByIp

{????????

/**

*?

*?@param?IP

*?@return

*/

public?static?String?GetAddressByIp(String?IP){

String?resout?=?"";

try{

String?str?=?getJsonContent(""+IP);

System.out.println(str);

JSONObject?obj?=?JSONObject.fromObject(str);

JSONObject?obj2?=??(JSONObject)?obj.get("data");

String?code?=?(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(Exception?e){

e.printStackTrace();

resout?=?"獲取IP地址異常:"+e.getMessage();

}

return?resout;

}

public?static?String?getJsonContent(String?urlStr)

{

try

{//?獲取HttpURLConnection連接對象

URL?url?=?new?URL(urlStr);

HttpURLConnection?httpConn?=?(HttpURLConnection)?url.openConnection();

//?設(shè)置連接屬性

httpConn.setConnectTimeout(3000);

httpConn.setDoInput(true);

httpConn.setRequestMethod("GET");

//?獲取相應(yīng)碼

int?respCode?=?httpConn.getResponseCode();

if?(respCode?==?200)

{

return?ConvertStream2Json(httpConn.getInputStream());

}

}

catch?(MalformedURLException?e)

{

e.printStackTrace();

}

catch?(IOException?e)

{

e.printStackTrace();

}

return?"";

}

private?static?String?ConvertStream2Json(InputStream?inputStream)

{

String?jsonStr?=?"";

//?ByteArrayOutputStream相當(dāng)于內(nèi)存輸出流

ByteArrayOutputStream?out?=?new?ByteArrayOutputStream();

byte[]?buffer?=?new?byte[1024];

int?len?=?0;

//?將輸入流轉(zhuǎn)移到內(nèi)存輸出流中

try

{

while?((len?=?inputStream.read(buffer,?0,?buffer.length))?!=?-1)

{

out.write(buffer,?0,?len);

}

//?將內(nèi)存流轉(zhuǎn)換為字符串

jsonStr?=?new?String(out.toByteArray());

}

catch?(IOException?e)

{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

return?jsonStr;

}

}

JAVA怎么獲取IP地址

這個是獲取不到的,因為有代理、端口映射等等轉(zhuǎn)發(fā)情況的存在。為什么不保存相對路徑/域名/或者在服務(wù)器上某個配置文件中配置域名/數(shù)據(jù)庫中一個表/數(shù)據(jù)庫中某個字段保存當(dāng)前服務(wù)器的ip地址呢?

java如何查詢本機ip地址和mac地址

Java中可以使用程序來獲取本地ip地址和mac地址,使用InetAddress這個工具類,示例如下:

import?java點虐 .*;

public?class?NetInfo?{

public?static?void?main(String[]?args)?{

new?NetInfo().say();

}

public?void?say()?{

try?{

InetAddress?i?=?InetAddress.getLocalHost();

System.out.println(i);??????????????????//計算機名稱和IP

System.out.println(i.getHostName());????//名稱

System.out.println(i.getHostAddress());?//只獲得IP

}

catch(Exception?e){e.printStackTrace();}

}

}

也可以通過命令行窗口來查看本地ip和mac地址,輸入命令:ipconfig。

新聞名稱:java代碼查詢ip地址 java如何獲取ip地址
網(wǎng)站鏈接:http://bm7419.com/article28/ddehccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)網(wǎng)站改版、網(wǎng)站維護、自適應(yīng)網(wǎng)站虛擬主機、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)