1.得到局域網(wǎng)網(wǎng)段,可由自己機(jī)器的IP來(lái)確定 (也可以手動(dòng)獲取主機(jī)IP-CMD-ipconfig /all)
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、網(wǎng)站建設(shè)、單縣網(wǎng)絡(luò)推廣、重慶小程序開(kāi)發(fā)公司、單縣網(wǎng)絡(luò)營(yíng)銷、單縣企業(yè)策劃、單縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供單縣建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
2.根據(jù)IP類型,一次遍歷局域網(wǎng)內(nèi)IP地址
JAVA類,編譯之后直接運(yùn)行便可以得到局域網(wǎng)內(nèi)所有IP,具體怎樣使用你自己編寫(xiě)相應(yīng)代碼調(diào)用便可
代碼如下::
package bean;
import java.io.*;
import java.util.*;
public class Ip{
static public HashMap ping; //ping 后的結(jié)果集
public HashMap getPing(){ //用來(lái)得到ping后的結(jié)果集
return ping;
}
//當(dāng)前線程的數(shù)量, 防止過(guò)多線程摧毀電腦
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30個(gè)線程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本機(jī)的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++){ //對(duì)所有局域網(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地址歸屬地,可以調(diào)用淘寶提供的service查詢,并且解析http請(qǐng)求返回的json串,代碼如下:
package?getAddressByIp;
import?java.io.ByteArrayOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java點(diǎn)虐 .HttpURLConnection;
import?java點(diǎn)虐 .MalformedURLException;
import?java點(diǎn)虐 .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連接對(duì)象
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;
}
}
這個(gè)是獲取不到的,因?yàn)橛写?、端口映射等等轉(zhuǎn)發(fā)情況的存在。為什么不保存相對(duì)路徑/域名/或者在服務(wù)器上某個(gè)配置文件中配置域名/數(shù)據(jù)庫(kù)中一個(gè)表/數(shù)據(jù)庫(kù)中某個(gè)字段保存當(dāng)前服務(wù)器的ip地址呢?
Java中可以使用程序來(lái)獲取本地ip地址和mac地址,使用InetAddress這個(gè)工具類,示例如下:
import?java點(diǎn)虐 .*;
public?class?NetInfo?{
public?static?void?main(String[]?args)?{
new?NetInfo().say();
}
public?void?say()?{
try?{
InetAddress?i?=?InetAddress.getLocalHost();
System.out.println(i);??????????????????//計(jì)算機(jī)名稱和IP
System.out.println(i.getHostName());????//名稱
System.out.println(i.getHostAddress());?//只獲得IP
}
catch(Exception?e){e.printStackTrace();}
}
}
也可以通過(guò)命令行窗口來(lái)查看本地ip和mac地址,輸入命令:ipconfig。