import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
public class IPUtil {
public static String[] getIPInfo(HttpServletRequest request) {
try {
String ip = getIpAddr(request);
String ipLocal = getAddress("ip=" + ip, "utf-8");
System.out.println(ip + ";" + ipLocal);
return new String[]{ip,ipLocal};
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取请求来的ips
*
* @param request
* @return
*/
private static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
// 这里主要是获取本机的ip,可有可无
if (ipAddress.equals("127.0.0.1")|| ipAddress.endsWith("0:0:0:0:0:0:1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
private static String getAddress(String params, String encoding) throws Exception {
String path = "http://ip.taobao.com/service/getIpInfo.php";
String returnStr = getRs(path, params, encoding);
JSONObject json = null;
if (returnStr != null) {
json = new JSONObject(returnStr);
if ("0".equals(json.get("code").toString())) {
StringBuffer buffer = new StringBuffer();
buffer.append(decodeUnicode(json.optJSONObject("data").getString("region")));// 省份
buffer.append(decodeUnicode(json.optJSONObject("data").getString("city")));// 市区
buffer.append(decodeUnicode(json.optJSONObject("data").getString("county")));// 地区
return buffer.toString();
} else {
return "获取地址失败";
}
}
return null;
}
/**
* 字符转码
*
* @param theString
* @return
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer buffer = new StringBuffer(len);
for (int i = 0; i < len;) {
aChar = theString.charAt(i++);
if (aChar == '\\') {
aChar = theString.charAt(i++);
if (aChar == 'u') {
int val = 0;
for (int j = 0; j < 4; j++) {
aChar = theString.charAt(i++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
val = (val << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
val = (val << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
val = (val << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed encoding.");
}
}
buffer.append((char) val);
} else {
if (aChar == 't') {
aChar = '\t';
}
if (aChar == 'r') {
aChar = '\r';
}
if (aChar == 'n') {
aChar = '\n';
}
if (aChar == 'f') {
aChar = '\f';
}
buffer.append(aChar);
}
} else {
buffer.append(aChar);
}
}
return buffer.toString();
}
/**
* 从url获取结果
*
* @param path
* @param params
* @param encoding
* @return
*/
private static String getRs(String path, String params, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(path);
connection = (HttpURLConnection) url.openConnection();// 新建连接实例
connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
connection.setDoInput(true);// 是否打开输出流 true|false
connection.setDoOutput(true);// 是否打开输入流true|false
connection.setRequestMethod("POST");// 提交方法POST|GET
connection.setUseCaches(false);// 是否缓存true|false
connection.connect();// 打开连接端口
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();// 关闭连接
}
return null;
}
public static void main(String[] args) throws Exception{
// String address = getAddress("ip=112.83.230.2", "utf-8");
String address = getAddress("ip=61.233.201.172", "utf-8");
System.out.println(address);
// getIPInfo(null);
}
}
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
public class IPUtil {
public static String[] getIPInfo(HttpServletRequest request) {
try {
String ip = getIpAddr(request);
String ipLocal = getAddress("ip=" + ip, "utf-8");
System.out.println(ip + ";" + ipLocal);
return new String[]{ip,ipLocal};
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取请求来的ips
*
* @param request
* @return
*/
private static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
// 这里主要是获取本机的ip,可有可无
if (ipAddress.equals("127.0.0.1")|| ipAddress.endsWith("0:0:0:0:0:0:1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
private static String getAddress(String params, String encoding) throws Exception {
String path = "http://ip.taobao.com/service/getIpInfo.php";
String returnStr = getRs(path, params, encoding);
JSONObject json = null;
if (returnStr != null) {
json = new JSONObject(returnStr);
if ("0".equals(json.get("code").toString())) {
StringBuffer buffer = new StringBuffer();
buffer.append(decodeUnicode(json.optJSONObject("data").getString("region")));// 省份
buffer.append(decodeUnicode(json.optJSONObject("data").getString("city")));// 市区
buffer.append(decodeUnicode(json.optJSONObject("data").getString("county")));// 地区
return buffer.toString();
} else {
return "获取地址失败";
}
}
return null;
}
/**
* 字符转码
*
* @param theString
* @return
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer buffer = new StringBuffer(len);
for (int i = 0; i < len;) {
aChar = theString.charAt(i++);
if (aChar == '\\') {
aChar = theString.charAt(i++);
if (aChar == 'u') {
int val = 0;
for (int j = 0; j < 4; j++) {
aChar = theString.charAt(i++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
val = (val << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
val = (val << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
val = (val << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed encoding.");
}
}
buffer.append((char) val);
} else {
if (aChar == 't') {
aChar = '\t';
}
if (aChar == 'r') {
aChar = '\r';
}
if (aChar == 'n') {
aChar = '\n';
}
if (aChar == 'f') {
aChar = '\f';
}
buffer.append(aChar);
}
} else {
buffer.append(aChar);
}
}
return buffer.toString();
}
/**
* 从url获取结果
*
* @param path
* @param params
* @param encoding
* @return
*/
private static String getRs(String path, String params, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(path);
connection = (HttpURLConnection) url.openConnection();// 新建连接实例
connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
connection.setDoInput(true);// 是否打开输出流 true|false
connection.setDoOutput(true);// 是否打开输入流true|false
connection.setRequestMethod("POST");// 提交方法POST|GET
connection.setUseCaches(false);// 是否缓存true|false
connection.connect();// 打开连接端口
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();// 关闭连接
}
return null;
}
public static void main(String[] args) throws Exception{
// String address = getAddress("ip=112.83.230.2", "utf-8");
String address = getAddress("ip=61.233.201.172", "utf-8");
System.out.println(address);
// getIPInfo(null);
}
}
//结果
新疆维吾尔自治区巴音郭楞蒙古自治州