java IP查询方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java IP查询方法相关的知识,希望对你有一定的参考价值。
老师给我们 布置的作业
如图 用eclipse 来做 跪求帮助啊
明天要交 的 摆脱各位大大了
help
Java编程查询IP地址归属地,可以调用淘宝提供的service查询,并且解析http请求返回的json串,代码如下:
package getAddressByIp;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONObject;
public class GetAddressByIp
/**
*
* @param IP
* @return
*/
public static String GetAddressByIp(String IP)
String resout = "";
try
String str = getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+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;
参考技术A public class NetTools
public static String getLocalHostIP()
String ip;
try
InetAddress addr = InetAddress.getLocalHost();
ip = addr.getHostAddress();
catch(Exception ex)
ip = "";
return ip;
public static String getLocalHostName()
String hostName;
try
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
catch(Exception ex)
hostName = "";
return hostName;
public static String[] getAllLocalHostIP()
String[] ret = null;
try
String hostName = getLocalHostName();
if(hostName.length()>0)
InetAddress[] addrs = InetAddress.getAllByName(hostName);
if(addrs.length>0)
ret = new String[addrs.length];
for(int i=0 ; i< addrs.length ; i++)
ret[i] = addrs[i].getHostAddress();
catch(Exception ex)
ret = null;
return ret;
public static String[] getAllHostIPByName(String hostName)
String[] ret = null;
try
if(hostName.length()>0)
InetAddress[] addrs = InetAddress.getAllByName(hostName);
if(addrs.length>0)
ret = new String[addrs.length];
for(int i=0 ; i< addrs.length ; i++)
ret[i] = addrs[i].getHostAddress();
catch(Exception ex)
ret = null;
return ret;
public static void main(String[] args)
//System.out.println(getLocalHostIP());
System.out.println("主机名:" + getLocalHostName());
String[] localIP = getAllLocalHostIP();
for(int i=0 ; i<localIP.length ; i++)
System.out.println( localIP[i]);
本回答被提问者采纳 参考技术B 调用request对象的getRemoteAddr方法可以获得客户请求中的ip地址。 参考技术C 如图? 图呢?追问
图不会上 不过重新写了文字
[命令行] curl查询本机公网出口IP
国内可用的查询本机公网IP方法
方法一
curl cip.cc
方法二
curl myip.ipip.net
国外可以用的查询本机公网IP方法
curl ipinfo.io
curl cip.cc
curl myip.ipip.net
curl ifconfig.me
以上是关于java IP查询方法的主要内容,如果未能解决你的问题,请参考以下文章