知道IP,获取当前IP归属地的Java程序:

package Main;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL; import ExtendElements.ExtendTest;
import ExtendElements.subElements; public class MainTest {
public static void main(String[] args) {
// 测试ip 10.0.0.7 内网
String ip = "10.0.0.7";
String address = "";
try {
address = getAddresses("ip=" + ip, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(address);
} /**
* 3 * @param content 请求的参数 格式为:name=xxx&pwd=xxx
*
* @param encoding 服务器端请求编码。如GBK,UTF-8等
* @return
* @throws UnsupportedEncodingException
*/
public static String getAddresses(String content, String encodingString) throws UnsupportedEncodingException {
// 这里调用pconline的接口
String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
// 从http://whois.pconline.com.cn取得IP所在的省市区信息
String returnStr = getResult(urlStr, content, encodingString);
if (returnStr != null) {
// 处理返回的省市区信息
System.out.println("IP=====" + returnStr);
String[] temp = returnStr.split(",");
if (temp.length < 3) {
return "0"; // 无效IP,局域网测试
}
String region = (temp[5].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region); // 省
System.out.println("region = " + region); String country = "";
String area = "";
// String region = "";
String city = "";
String county = "";
String isp = "";
System.out.println("temp的长度=" + temp.length);
for (int i = 0; i < temp.length; i++) {
switch (i) {
// 如果使用的是新浪的接口,那这里的需要修改,case:3 4 5分别对应国家,省,市区
case 1:
country = (temp[i].split(":"))[2].replaceAll("\"", "");
country = decodeUnicode(country); // 国家
break;
case 3:
area = (temp[i].split(":"))[1].replaceAll("\"", "");
area = decodeUnicode(area); // 地区
break;
case 5:
region = (temp[i].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region); // 省份
break;
case 7:
city = (temp[i].split(":"))[1].replaceAll("\"", "");
city = decodeUnicode(city); // 市区
break;
case 9:
county = (temp[i].split(":"))[1].replaceAll("\"", "");
county = decodeUnicode(county); // 地区
break;
case 11:
isp = (temp[i].split(":"))[1].replaceAll("\"", "");
isp = decodeUnicode(isp); // ISP公司
break;
}
}
System.out.println(country + "=" + area + "=" + region + "=" + city + "=" + county + "=" + isp);
return region;
}
return null;
} /**
* @param urlStr 请求的地址
* @param content 请求的参数 格式为:name=xxx&pwd=xxx
* @param encoding 服务器端请求编码。如GBK,UTF-8等
* @return
*/
private static String getResult(String urlStr, String content, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection(); // 新建连接实例
connection.setConnectTimeout(2000); // 设置连接超时时间,单位毫秒
connection.setReadTimeout(2000); // 设置读取数据超时时间,单位毫秒
connection.setDoOutput(true); // 是否打开输出流 true|false
connection.setDoInput(true); // 是否打开输入流true|false
connection.setRequestMethod("POST"); // 提交方法POST|GET
connection.setUseCaches(false); // 是否缓存true|false
connection.connect(); // 打开连接端口
DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打开输出流往对端服务器写数据
out.writeBytes(content); // 写数据,也就是提交你的表单 name=xxx&pwd=xxx
out.flush(); // 刷新
out.close(); // 关闭输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据
// ,以BufferedReader流来读取
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect(); // 关闭连接
}
}
return null;
} /**
* unicode 转换成 中文
*
* @author fanhui 2007-3-15
* @param theString
* @return
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException("Malformed encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't') {
aChar = '\t';
} else if (aChar == 'r') {
aChar = '\r';
} else if (aChar == 'n') {
aChar = '\n';
} else if (aChar == 'f') {
aChar = '\f';
}
outBuffer.append(aChar);
}
} else {
outBuffer.append(aChar);
}
}
return outBuffer.toString();
}
}
221.232.245.73

Java-获取当前IP归属地的更多相关文章

  1. Java获取用户ip

    /** * 获取客户端ip地址(可以穿透代理) * * @param request * @return */ public static String getRemoteAddr(HttpServl ...

  2. java获取服务器IP地址及MAC地址的方法

    这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下   本文实例讲述了 ...

  3. JAVA获取客户端IP地址

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  4. Java获取本地IP地址

    import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...

  5. JAVA获取客户端IP地址和MAC地址

    1.获取客户端IP地址 public String getIp(HttpServletRequest request) throws Exception { String ip = request.g ...

  6. Java获取本地IP地址和主机名

    方式一:通过java.net.InetAddress类获取 public void test1() { try { InetAddress addr = InetAddress.getLocalHos ...

  7. Java 获取客户端ip返回127.0.0.1问题

    Java开发中使用 request.getRemoteAddr 获取客户端 ip ,返回结果始终为127.0.0.1.原因是服务器使用了nginx反向代理. 解决办法:在nginx配置文件nginx. ...

  8. Java获取电脑IP、MAC、各种版本

    Java代码获取电脑IP.MAC.各种版本 package com.rapoo.middle.action; import java.io.BufferedReader; import java.io ...

  9. Java获取网络IP

    Java获取获取网络IP,浅尝辄止咯- import java.net.InetAddress; import java.net.UnknownHostException; /** * 获取网络IP ...

  10. JAVA获取公网ip

    在ipv4地址稀缺的今天,分配到公网ip几乎是不可能的,但是我拨号之后的ip竟然是公网IP. 将自己的电脑作为服务器·,做点好玩的程序,就成为了可能. 由于运营商的ip是动态分配的公网ip的所以就需要 ...

随机推荐

  1. CSS字体中英文名称对照表

    在CSS文件中,我们常看到有些字体名称变成了乱码,这是由于编写者将中文字体的名字直接写成了中文,并且再上传或者拷贝复制的时候无意间变成了乱码. 为了避免这种状况出现,在CSS文件中使用中文字体时,最好 ...

  2. SpringBoot使用JPA来做数据查询

    Spring-Data-JPA在做数据存储方面真的很方便,它的目的就是写更少的代码,更多的事情,但是也有其力有未逮或者说处理起来比较闹心的地方. 1.先来感受一下使用JPA做数据查询时,代码的简化程度 ...

  3. axios 请求多个接口

    axios.all([ axios.get('https://api.github.com/xxx/1'), axios.get('https://api.github.com/xxx/2') ]) ...

  4. 第七章 路由 75 路由传参-使用query方式传递参数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  5. 《Python基础教程》第二章:列表和元组(2)

    list函数可以将字符串转换为列表 ' '.join(somelist)可以将列表转换为字符串 从列表中删除元素可以使用del语句来实现 方法是与对象有紧密联系的函数:对象.方法(参数) append ...

  6. IIS无法启动解决方案

    1.第一步 2.第二步

  7. substring和substr的区别

    substring和subsrt都是获取指定位数 字符串的方法: 语法: substring(start,end)/substring(one); substr(start,end)/substr(o ...

  8. [Algorithm] Finding Prime numbers - Sieve of Eratosthenes

    Given a number N, the output should be the all the prime numbers which is less than N. The solution ...

  9. [Python自学] day-16 (JS、作用域、DOM、事件)

    一.JS中的三种函数 1.普通函数 function func(){ console.log("Hello World"); } func() 2.匿名函数 setInterval ...

  10. VirtualBox:启动虚拟机后计算机死机

    造冰箱的大熊猫@cnblogs 2018/2/21 故障描述:Ubuntu 16.04升级Linux内核后,在VirtualBox中启动虚拟机发现Ubuntu死机,只能通过长按电源开关硬关机的方式关闭 ...