Java-IPDeal工具类
2024-09-03 14:11:33
/**
* 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());
}
}
最新文章
- Akka初步介绍
- jquery触屏幻灯片
- 针对APP的测试过程和重点关注内容,做以下梳理和总结
- ROS 新手教程 命令汇总
- 使用git建立远程仓库,让别人git clone下来
- NOIP2009普及组细胞分裂(数论)——yhx
- 解决SaveChanges会Hold住之前的错误的问题
- 【Anagrams】 cpp
- java Servlet接口及应用
- ASP.net MVC基础
- OpenCV2学习笔记01:Linux下OpenCV开发环境的搭建
- Oracle DB 备份和恢复的概念
- Vector/Arraylist与Linklist的区别
- JS笔记2 --定义对象
- jQuery停止事件冒泡
- R语言学习笔记之外部文件读取
- GeoServer初识与安装
- Java 多线程之线程池的使用
- 微信小程序,加载更多
- R语言-箱型图&;热力图
热门文章
- Socket与系统调用深层分析
- 10.hive安装
- #import ";msado15.dll"; no_namespace rename(";EOF";,";adoEOF";)
- 剑指offer3:从尾到头打印链表每个节点的值
- INPUT和CONSTRUCT指令——范例报表查询,作用让用户输入数据,自动生成SQL的WHERE条件,带开窗查询
- maraidb忘记数据密码
- List与Set区别
- ZROIDay3-比赛解题报告
- HTML5之动画优化(requestAnimationFrame)
- Golang新开发者要注意的陷阱和常见错误