博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IPUtil
阅读量:7028 次
发布时间:2019-06-28

本文共 2137 字,大约阅读时间需要 7 分钟。

package com.mz.base.util;

import java.text.ParseException;

import org.apache.commons.lang3.StringUtils;

import org.json.JSONObject;

/**

* IP帮助类
* @author zejun
*/
public class IPUtil {
/**
* IP地址转详细地址
* @param ip
* @param apiName 取值范围:taobao、
* @return
* country 国家
* area 区域,片区
* province 省份
* city 城市
* county 区县
* isp 网络服务商
*/
public static JSONObject ipToLocation(String ip, String apiName){
if(StringUtils.isEmpty(apiName)){
apiName = "taobao";
}
JSONObject json = new JSONObject();
if("taobao".equals(apiName)){
try {
String url = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip;
String result = UrlUtils.readFromURL(url, "UTF-8", 1024, 10000, null);
JSONObject resultJson = new JSONObject(result);
resultJson = resultJson.getJSONObject("data");
json.put("code", 1);
json.put("country", JSONUtil.getString(resultJson, "country"));
json.put("area", JSONUtil.getString(resultJson, "area"));
json.put("province", JSONUtil.getString(resultJson, "region"));
json.put("city", JSONUtil.getString(resultJson, "city"));
json.put("county", JSONUtil.getString(resultJson, "county"));
json.put("isp", JSONUtil.getString(resultJson, "isp"));
} catch (ParseException e) {
e.printStackTrace();
}
}else if("sina".equals(apiName)){
try {
String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ip;
String result = UrlUtils.readFromURL(url, "UTF-8", 1024, 10000, null);
JSONObject resultJson = new JSONObject(result);
json.put("code", 1);
json.put("country", JSONUtil.getString(resultJson, "country"));
json.put("province", JSONUtil.getString(resultJson, "province"));
json.put("city", JSONUtil.getString(resultJson, "city"));
json.put("county", JSONUtil.getString(resultJson, "district"));
json.put("isp", JSONUtil.getString(resultJson, "isp"));
} catch (ParseException e) {
e.printStackTrace();
}
}
return json;
}
public static void main(String[] args) {
/*String url = "http://ip.ws.126.net/ipquery?ip=169.235.24.133";
System.out.println(UrlUtils.readFromURL(url, "GBK", 1024, 10000, null));*/
System.out.println(IPUtil.ipToLocation("169.235.24.133", null));
}
}

转载于:https://www.cnblogs.com/yufeng1102/p/7536290.html

你可能感兴趣的文章
keePass
查看>>
MVVM模式下弹出窗体
查看>>
URAL 1018 Binary Apple Tree
查看>>
MYSQL中char 与 varchar 的区别
查看>>
算法设计与分析基础 (Anany Levitin 著)
查看>>
BackBone 源码解读及思考
查看>>
Mybatis四种分页方式
查看>>
查找-二分法查找(折半查找法)
查看>>
svn is already locked解决方案
查看>>
Oracle合并某一列
查看>>
C语言结构体知识
查看>>
centos安装ftp
查看>>
Viewpager+fragment数据更新问题解析
查看>>
多线程实现socketserver练习
查看>>
ElasticSearch 模板文件配置
查看>>
leetcode–Binary Tree Maximum Path Sum
查看>>
字节对齐总结
查看>>
[LeetCode]22. Generate Parentheses括号生成
查看>>
idea tomcat 乱码问题的解决及相关设置
查看>>
centos7 yum搭建lamp
查看>>