package cn.ksource.core.util;
|
|
import java.net.InetAddress;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import cn.ksource.core.CascadeMap;
|
|
public class IPUtil {
|
|
public static String getIpAddr(HttpServletRequest request) {
|
String ip = request.getHeader("x-forwarded-for");
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
return ip;
|
}
|
|
/**
|
* 获取本机IP地址
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @throws HttpRemoteException
|
* @date Jan 25, 2014 2:21:05 PM
|
*/
|
public static IPEntity getIpEntity() throws HttpRemoteException{
|
IPEntity ipEntity = new IPEntity();
|
ipEntity.setLanIP(getLocalHostIp());
|
String content = HttpUtil.doGet("http://pv.sohu.com/cityjson", null, HttpCharset.GBK);
|
content = StringUtils.removeStart(content, "var returnCitySN = ");
|
content = StringUtils.removeEnd(content, ";");
|
Map ipMap = JsonUtil.json2Map(content);
|
ipEntity.setInternetIP(ConvertUtil.obj2Str(ipMap.get("cip")));
|
ipEntity.setPosition(ConvertUtil.obj2Str(ipMap.get("cname")));
|
return ipEntity;
|
}
|
|
public static void main(String[] args) throws HttpRemoteException {
|
String content = HttpUtil.doGet("http://pv.sohu.com/cityjson", null, HttpCharset.GBK);
|
content = StringUtils.removeStart(content, "var returnCitySN = ");
|
content = StringUtils.removeEnd(content, ";");
|
|
Map ipMap = JsonUtil.json2Map(content);
|
System.out.println(ipMap.get("cid"));
|
|
String ip = content.replaceAll(":",":");
|
|
System.out.println(content);
|
System.out.println(ip);
|
}
|
|
|
/**
|
* 获取本机IP地址,操作系统为Windows,Linux上返回127.0.0.1
|
* 杨凯
|
* @return
|
*/
|
public static String getLocalHostIp(){
|
String ip ="";
|
|
try {
|
ip = InetAddress.getLocalHost().getHostAddress();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return ip;
|
}
|
|
/**
|
* 是否内网IP
|
* @param ipAddress
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 25, 2014 1:29:57 PM
|
*/
|
public static boolean isInnerIP(String ipAddress) {
|
|
if (ipAddress.equals("127.0.0.1")) {
|
return true;
|
}
|
|
long ipNum = getIpNum(ipAddress);
|
|
/**
|
* 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类
|
* 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
|
*/
|
List<Map<String, String>> ipList = new ArrayList<Map<String,String>>();
|
ipList.add(new CascadeMap().put("START_IP", "10.0.0.0").put("END_IP", "10.255.255.255"));
|
ipList.add(new CascadeMap().put("START_IP", "172.16.0.0").put("END_IP", "172.31.255.255"));
|
ipList.add(new CascadeMap().put("START_IP", "192.168.0.0").put("END_IP", "192.168.255.255"));
|
ipList.add(new CascadeMap().put("START_IP", "1.1.0.0").put("END_IP", "1.1.255.255"));
|
|
|
for (Map<String, String> map : ipList) {
|
if ((ipNum >= getIpNum(map.get("START_IP")) && ipNum <= getIpNum(map.get("END_IP")))) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
private static long getIpNum(String ipAddress) {
|
String[] ip = ipAddress.split("\\.");
|
long a = Integer.parseInt(ip[0]);
|
long b = Integer.parseInt(ip[1]);
|
long c = Integer.parseInt(ip[2]);
|
long d = Integer.parseInt(ip[3]);
|
|
long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
|
return ipNum;
|
}
|
|
|
}
|