IP相关工具类
IP相关工具类
shawntime 发表于2年前
IP相关工具类
  • 发表于 2年前
  • 阅读 22
  • 收藏 1
  • 点赞 1
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;

/**
 * Created by IDEA
 * User: mashaohua
 * Date: 2016-04-13 9:13
 * Desc:
 */
public class IPUtil {

    /**
     * 获取客户端ip
     *
     */
    public static String getIp(HttpServletRequest request) {

        String ipAddress = request.getHeader("x-forwarded-for");

        if(!verifyIp(ipAddress)) {
            ipAddress = request.getHeader("X-Real-IP");
            if(!verifyIp(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
                if(!verifyIp(ipAddress)) {
                    ipAddress = request.getHeader("WL-Proxy-Client-IP");
                    if(!verifyIp(ipAddress)) {
                        ipAddress = request.getRemoteAddr();
                        if(ipAddress.equals("127.0.0.1")){
                            //根据网卡取本机配置的IP
                            InetAddress inet = null;
                            try {
                                inet = InetAddress.getLocalHost();
                                ipAddress = inet.getHostAddress();
                            } catch (UnknownHostException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
        //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
        if(ipAddress != null && ipAddress.length() > 15){ //"***.***.***.***".length() = 15
            if(ipAddress.indexOf(",") > 0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
            }
        }

        return ipAddress;
    }


    private final static Pattern ipPattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");

    public static boolean verifyIp(String ip) {
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip))
            return false;

        return ipPattern.matcher(ip).matches();
    }

    public static long ipToLong(String ip){
        long[] array = new long[4];
        int position1 = ip.indexOf(".");
        int position2 = ip.indexOf(".", position1 + 1);
        int position3 = ip.indexOf(".", position2 + 1);
        array[0] = Long.parseLong(ip.substring(0, position1));
        array[1] = Long.parseLong(ip.substring(position1+1, position2));
        array[2] = Long.parseLong(ip.substring(position2+1, position3));
        array[3] = Long.parseLong(ip.substring(position3+1));
        return (array[0] << 24) + (array[1] << 16) + (array[2] << 8) + array[3];
    }

    public static String ipToString(long ip){
        StringBuffer sb = new StringBuffer("");
        sb.append(String.valueOf((ip >>> 24)));
        sb.append(".");
        sb.append(String.valueOf((ip & 0x00FFFFFF) >>> 16));
        sb.append(".");
        sb.append(String.valueOf((ip & 0x0000FFFF) >>> 8));
        sb.append(".");
        sb.append(String.valueOf((ip & 0x000000FF)));
        return sb.toString();
    }
}


共有 人打赏支持
粉丝 33
博文 75
码字总数 79249
×
shawntime
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: