纯真IP数据包含381085条,可以通过下载的查询软件将数据解压为文本格式,并将其编码改为UTF8,否则在程序中读取中文会乱码!
下面为程序执行分析IP数据并插入到Sql Server的截图:
程序通过AJAX在客户端进行数据插入实时更新:
实现代码如下:
前端页面及javascript:
复制代码 代码如下:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
<title>导入IP地址数据库-power by blog.atnet.cc</title>
<style type=”text/css”>
body{font-size:14px;}
#log{border:solid 1px gold;width:400px;height:100px;padding:10px;background:gold;margin-bottom:15px;color:black;}
#recordLog{font-size:12px;}
</style>
<script type=”text/javascript” src=”/scripts/global.js”></script>
<script type=”text/javascript”>
var log,reLog; //Log,RecordLog
var recordCount; //IP记录总数
window.onload=function(){
log=document.getElementById(“log”);
}
function startImport(){
if(!document.getElementById(“submit_ifr”)){
var elem=document.createElement(“iframe”);
elem.setAttribute(“id”,”submit_ifr”);
elem.setAttribute(“name”,”ifr”);
elem.style.cssText=”display:none”;
document.body.appendChild(elem);
document.forms[0].target=elem.name;
}
document.forms[0].submit();
log.innerHTML=”正在上传数据!<br />”;
return false;
}
�
function insertIP(){
log.innerHTML+=”开始分析数据…<br />”;
j.ajax.post(“/do.ashx?args=ImportIPData&action=init”,”",
function(x){
var d=eval(x)[0];
recordCount=d.count;
log.innerHTML+=”<font color=green>分析数据成功:<br />服务器地址:”+
d.server+”,记录:”+recordCount+”条!<br /><div id="recordLog"></div>”;
//开始插入
insert();
},
function(x){log.innerHTML+=”<font color=red>发生异常,已终止!</font>”;}
);
}
function insert(){
if(!reLog)reLog=document.getElementById(“recordLog”);
var num=Math.floor(Math.random()*100);
j.ajax.post(“/do.ashx?args=ImportIPData&action=insert”,”num=”+num,
function(x){var d=eval(x)[0];reLog.innerHTML=”已经写入数据:”+(recordCount-d.count)+
“条,队列:”+d.count+”条,本次写入:”+d.insertNum+”条”;
if(d.count!=0){insert();}
else{reLog.innerHTML=”恭喜,写入完毕!”;}
},function(x){alert(x);});
}
</script>
</head>
<body>
<div style=”margin:60px 100px”>
<div id=”log”>请填写相关数据,选择IP数据文件!</div>
<form action=”/do.ashx?args=ImportIPData” method=”post” enctype=”multipart/form-data” target=”ifr”>
数据库IP:<input type=”text” name=”dbserver” value=”.” /><br />
数据库名:<input type=”text” name=”dbname” value=”tp” /><br />
数据表名:<input type=”text” name=”tbname” value=”ip” /><br />
用 户 名:<input type=”text” name=”dbuid” value=”sa” /><br />
密 码<input type=”password” name=”dbpwd” value=”123000″ /><br />
IP文件:<input type=”file” name=”ipfile” value=”C:\Users\cwliu\Desktop\1.txt” /><br />
<button onclick=”return startImport();”>导入</button>
</form>
</div>
</body>
</html>
注:j为一个自定义的javascript类库,中间包含了ajax功能的代码
后台程序我们用来接收ajax发送的Post 请求:
代码如下:
复制代码 代码如下:
File:do.ashx?args=ImportIPData
public vo