大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
java用正则表达式统计ip个数:
成都创新互联公司从2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元长子做网站,已为上家服务,为长子各地企业和个人服务,联系电话:18980820575
display ip count rule [ | { begin | exclude | include } regular-expression ]
参数说明:
|:使用正则表达式对显示信息进行过滤。有关正则表达式的详细介绍,请参见“入门配置指导”中的“CLI”。
begin:从包含指定正则表达式的行开始显示。
exclude:只显示不包含指定正则表达式的行。
include:只显示包含指定正则表达式的行。
regular-expression:表示正则表达式,为1~256个字符的字符串,区分大小写。
【举例】
# 显示用户配置的IP Accounting统计规则。
Sysname display ip count rule
IP Count rule list:
IP address address mask
1.1.1.0 255.255.255.0
2.0.0.0 255.0.0.0
------------------------------------
Total: 2 rules
步骤一、建一个表,表名任意,这里取名为:visitorcounter,表的结构如下所示:
+-------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| vdate | date | NO | | 2000-01-01 | |
| vnum | int(11) | NO | | 0 | |
+-------+------------------+------+-----+------------+----------------+
步骤二、建立一个java类,名字也为:visitorcounter,类的内容如下:
package com.hdzx.pub;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
public class VisitorCounter {
private final static String TABLE_NAME = "visitorcounter";
private static String today = null;
private static long today_num = 0;
private static long total_num = 0;
//加载访问量
public static void loadNum(){
if(total_num1)
loadTotalNum();
if(today_num1)
loadToadyNum();
}
//加载今日访问量
private static void loadToadyNum() {
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where vdate='"+today+"'";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
today_num = rs.getLong("vnum");
}
else
{
sql = "insert into "+TABLE_NAME+"(vdate,vnum) values('"+today+"',0)";
db.executeUpdate(sql);
today_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
today_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
}
}
//加载总访问量
private static void loadTotalNum() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where id=1";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
total_num = rs.getLong("vnum");
}
else
{
total_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
total_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
}
}
//增加总的访问量
private static int incTotalCounter(){
int k = 0;
DBConnect db = null;
loadNum();
total_num = total_num+1;
String sql = "update "+TABLE_NAME+" set vnum="+total_num+" where id=1";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
}
return k;
}
//增加今日的访问量
public static int incTodayCounter(){
int k = 0;
DBConnect db = null;
String sql = null;
loadNum();
today_num += 1;
sql = "update "+TABLE_NAME+" set vnum="+today_num+" where vdate='"+today+"'";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
if(k 0)
incTotalCounter();
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
}
return k;
}
//获得今天的日期
private static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
///获得今日访问量
public static long getTodayNum(){
loadNum();
return today_num;
}
//获得总的访问量
public static long getTotalNum(){
loadNum();
return total_num;
}
}
步骤三、经过以上的步骤后,在页面中加入以下的代码,就可以实现网站访问量的统计工作:
if(session.isNew())
{
VisitorCounter.incTodayCounter();
}
%
今日访问量:%=VisitorCounter.getTodayNum() %br/
总的访问量: %=VisitorCounter.getTotalNum() %
1.得到局域网网段,可由自己机器的IP来确定 (也可以手动获取主机IP-CMD-ipconfig /all)
2.根据IP类型,一次遍历局域网内IP地址
JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可
代码如下::
package bean;
import java.io.*;
import java.util.*;
public class Ip{
static public HashMap ping; //ping 后的结果集
public HashMap getPing(){ //用来得到ping后的结果集
return ping;
}
//当前线程的数量, 防止过多线程摧毁电脑
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30个线程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本机的IP,得到网段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i =255;i++){ //对所有局域网Ip
String iip=ss+i;
Ping(iip);
}
//等着所有Ping结束
while(threadCount0)
Thread.sleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();
String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
if(value.equals("true"))
System.out.println(key+"--"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
this.ip=ip;
}
public void run(){
try{
Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
//读取结果行
for (int i=1 ; i 7; i++)
input.readLine();
String line= input.readLine();
if (line.length() 17 || line.substring(8,17).equals("timed out"))
ping.put(ip,"false");
else
ping.put(ip,"true");
//线程结束
threadCount -= 1;
}catch (IOException e){}
}
}
}
在web.xml中加入获取访客的filter,然后获取IP地址,查询数据库此IP是否有记录,如果无记录,则进行访问计数+1,并更新数据库相应数据,这样就可以达到计算次数的操作了