Java根据IP地址获取物理地址(通过淘宝API接口)

原创
2020/04/07 16:53
阅读数 1.1K

1. 创建一个Java工具类IpAddressUtil;

2. 写入以下代码:

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 javax.servlet.http.HttpServletRequest;

public class IpAddressUtil {
	
	public String getAddresses(String content, String encodingString) 
			throws UnsupportedEncodingException {
		// 这里调用淘宝的接口
		String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
		// 从接口取得IP所在的省市区信息
		String returnStr = this.getResult(urlStr, content, encodingString);
		if (returnStr != null) {
			// 处理返回的省市区信息
			System.out.println(returnStr);
			String[] temp = returnStr.split(",");
			if(temp.length<3){
				return "0";//无效IP,局域网测试
			}
			// String region = (temp[5].split(":"))[1].replaceAll("\"", "");
			// region = decodeUnicode(region);// 省份
			String country = (temp[5].split(":"))[0].replaceAll("\"", "");
			String city = (temp[5].split(":"))[1].replaceAll("\"", "");
			String region = country + "," + city;
			return region;
		}
		return null;
	}
	
	private 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;
	}
	
	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();
	}
	
	public static String getAddressResult(String ip) {
		// 创建默认http连接
		HttpClient client = HttpClients.createDefault();
		// 创建一个post请求
		HttpPost post = new HttpPost("http://api.map.baidu.com/location/ip");
		List<NameValuePair> paramList = new ArrayList<NameValuePair>();
		// 传递的参数
		paramList.add(new BasicNameValuePair("ip", ip));
		paramList.add(new BasicNameValuePair("ak", "npaDNxTGE3NSmcVe2CW825DHCOPSwFFk"));
		paramList.add(new BasicNameValuePair("sn", ""));
		paramList.add(new BasicNameValuePair("coor", ""));
		
		String address = "";
		try {
			// 把参转码后放入请求实体中
			HttpEntity entitya = new UrlEncodedFormEntity(paramList, "utf-8");
			post.setEntity(entitya);// 把请求实体放post请求中
			// 用http连接去执行get请求并且获得http响应
			HttpResponse response = client.execute(post);
			// 从response中取到响应实体
			HttpEntity entity = response.getEntity();
			// 把响应实体转成文本
			String str = EntityUtils.toString(entity);
			
			// 分隔解析
			if(str.length() >0) {
				int index = str.indexOf("province");
				int index2 = str.indexOf("city");
				int index3 = str.indexOf("district");
				String province = str.substring(index+11, index2-3);
				String city = str.substring(index2+7, index3-3);
				// System.out.println(province);
				// System.out.println(city);
				address = decodeUnicode(province) + "," + decodeUnicode(city);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return address;
	}
    
	// 测试
	public static void main(String[] args) throws IOException {
		IpAddressUtil ipAddressUtil = new IpAddressUtil();
		String ip = "59.42.239.26";
		String address = "";
		
		// 淘宝API
		try {
			address = ipAddressUtil.getAddresses("ip="+ip, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println(address);
	}
}

3. 在需要的地方调用即可

4. 淘宝API接口优缺点:

优点:

(1)提供国家、省、市、县、运营商全方位信息,信息维度广,格式规范;

(2)提供完善的统计分析报表,省准确度超过99.8%,市准确度超过96.8%,数据质量有保障。

缺点:

淘宝API接口限制很多,为了保障服务正常运行,每个用户的访问频率需小于10qps

5. 传送门:

通过Java获取IP地址: https://my.oschina.net/u/3986411/blog/3223248

Java根据IP地址获取物理地址(通过百度API接口): https://my.oschina.net/u/3986411/blog/3223308

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部