zoukankan      html  css  js  c++  java
  • Java-IPDeal工具类

    /**
     * IP处理类
     */
    import java.sql.*;
    
    public class IPDeal {
        /**
         * 通过ip地址查询地区名称
         * @param strip
         * @return ip所在地区名称
         */
        public static String getArea(String strip){
            Connection conn = null;
            PreparedStatement pstmt = null;
            String sql;
            String strRtn = null;
            try{
                MyJdbc myjdbc = new MyJdbc();
                conn = myjdbc.getConn();
                sql = "select * from fullip where startip<='" + strip + "' and endip>='" + strip + "'";
                pstmt = conn.prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();
                if(rs.next()){
                    strRtn = rs.getString("country");
                }else{
                    strRtn = "未确定";
                }
                rs.close();
                rs = null;
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if (pstmt != null)
                    try{
                        pstmt.close();
                        pstmt = null;
                    }catch(Exception e){}
                if (conn != null)
                    try{
                        conn.close();
                        conn = null;
                    }catch(Exception e){}
            }
            return strRtn;
        }
        /**
         * 把ip地址格式化为:000.000.000.000
         * @param ip
         * @return 返回规格ip
         */
        public static String strfullip(String ip){
            StringBuffer buff = new StringBuffer();
            buff.append("");
            String strzero = "000";
            int ilen = 0;
            if(ip != null){
                String[] arrip = ip.split("\.");
                if(arrip.length == 4){
                    for(int i = 0; i < 4; i++){
                        if (i==0){
                            ilen = arrip[i].length();
                            if(ilen < 3){
                                buff.append(strzero.substring(0,3-ilen)).append(arrip[i]);
                            }else{
                                buff.append(arrip[i]);
                            }
                        }else{
                            ilen = arrip[i].length();
                            if(ilen < 3){
                                buff.append(".").append(strzero.substring(0,3-ilen)).append(arrip[i]);
                            }else{
                                buff.append(".").append(arrip[i]);
                            }
                        }
                    }
                }
            }
            return buff.toString();
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            String strip = "202.108.33.32";
            System.out.println(IPDeal.strfullip(strip));
            System.out.println(System.currentTimeMillis());
            System.out.println("ip" + strip + "所在地区:" + IPDeal.getArea(IPDeal.strfullip(strip)));
            System.out.println(System.currentTimeMillis());
        }
    
    }
    
  • 相关阅读:
    CDH环境搭建及部署
    Window 安装Sqoop 环境
    软件——keil的查找,错误,不能跳转到相应的行
    软件——机器学习与Python,输入输出的用法
    软件——机器学习与Python,聚类,K——means
    软件——机器学习与Python,if __name__ == '__main__':函数
    硬件——STM32,ADC篇
    生活的开始
    硬件——nrf51822第三篇,按键控制小灯
    硬件——nrf51822第二篇,如何设置keil用来下载程序
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210969.html
Copyright © 2011-2022 走看看