• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
山高我为峰
博客园    首页    新随笔    联系   管理    订阅  订阅
java 判断字符串IP合法性以及获取IP的数值形式
  /**
     * 计算传入的IP地址的数字IP*/
    public static long getIpNum(String ip) {
        long ipNum = 0;
        if (StringUtils.isNotBlank(ip) && isIP(ip)) {
            String[] spstr_IP = ip.split("\\.");
            ipNum = Long.parseLong(spstr_IP[0]) * 256 * 256 * 256 + Long.parseLong(spstr_IP[1]) * 256 * 256 + Long.parseLong(spstr_IP[2]) * 256 + Long.parseLong(spstr_IP[3]);
        }
        return ipNum;
    }
    
    /**
     * 判断是否是合法的IP地址*/
    public static boolean isIP(String ip){
        Pattern ipPattern=Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-1]\\d|22[0-3])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}");
        Matcher matcher=ipPattern.matcher(ip);
        return matcher.matches();
    }

 

posted on 2015-09-23 11:17  山高我为峰  阅读(2261)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3