资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码查询ip地址 java如何获取ip地址

如何获取局域网内所有IP地址 java代码

1.得到局域网网段,可由自己机器的IP来确定 (也可以手动获取主机IP-CMD-ipconfig /all)

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于网站制作、网站建设、单县网络推广、重庆小程序开发公司、单县网络营销、单县企业策划、单县品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供单县建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

2.根据IP类型,一次遍历局域网内IP地址

JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可

代码如下::

package bean;

import java.io.*;

import java.util.*;

public class Ip{

static public HashMap ping; //ping 后的结果集

public HashMap getPing(){ //用来得到ping后的结果集

return ping;

}

//当前线程的数量, 防止过多线程摧毁电脑

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,得到网段

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++){ //对所有局域网Ip

String iip=ss+i;

Ping(iip);

}

//等着所有Ping结束

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);

//读取结果行

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");

//线程结束

threadCount -= 1;

}catch (IOException e){}

}

}

}

java IP查询方法

Java编程查询IP地址归属地,可以调用淘宝提供的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();

// 设置连接属性

httpConn.setConnectTimeout(3000);

httpConn.setDoInput(true);

httpConn.setRequestMethod("GET");

// 获取相应码

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相当于内存输出流

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = 0;

// 将输入流转移到内存输出流中

try

{

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

{

out.write(buffer, 0, len);

}

// 将内存流转换为字符串

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

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

return jsonStr;

}

}

JAVA怎么获取IP地址

这个是获取不到的,因为有代理、端口映射等等转发情况的存在。为什么不保存相对路径/域名/或者在服务器上某个配置文件中配置域名/数据库中一个表/数据库中某个字段保存当前服务器的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地址
网页网址:http://cdkjz.cn/article/ddehccp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220