java 访问统计

package com.zrw.publicmessage.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import com.zrw.publicmessage.service.PublicMessageService;

public class Counter {

private String cs; //字符型访问计数数值;  

private int ci; //整形访问计数数值;  

private int counterSize; //访问计数器位数;  

private String HTMLCode; //HTML代码;  

private byte buffer[]; //输出缓存字节数组;  

private String tempStr; //字符型中间变量;  

private int tempInt; //整形中间变量;  

//private String logFileName; //信息记录文件名称;  

private String counterFileName; //访问计数文件名称;  

private FileOutputStream counterOut; //访问计数运算结果记录输出;  

private FileOutputStream infoOut; //信息记录输出;  

private FileInputStream counterIn; //访问计数记录输入;  

private DataInputStream dataRead; //以数据形式输入访问计数记录;  

// private String userAgent; //用户使用的浏览器;   
//
// private String userIP; //用户IP地址;   
//
// private Date userTime; //用户访问时间;   

private final static String FILE_PATH; // 文件保存路径

static {

   try {
    InputStream input = PublicMessageService.class.getClassLoader().getResourceAsStream("file_path.properties");
    Properties prop = new Properties();
    prop.load(input);
    FILE_PATH = prop.getProperty("com.zrw.publicmessage.vo.PublicMessageFile");
    File file = new File(FILE_PATH);
    if(!file.exists()){//文件夹不存在
     file.mkdir();
    }
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    throw new RuntimeException("读取文件保存路径错误");
   }
  
}

//   构造函数   
public Counter() {

   tempStr = null;
   tempInt = 0;

   //logFileName = "log.txt";
   counterFileName = FILE_PATH+"/counter.txt";

   cs = null;
   ci = 0;
   counterSize = 8;
   HTMLCode = "";

//   userAgent = null;
//   userIP = null;
//   userTime = new Date();

}

/*
//   访问信息记录文件设置函数   
public void setLogFileName(String filename) {
   logFileName = filename;
}
*/

//   访问计数记录文件设置函数   
public void setCounterFileName(String filename) {
   counterFileName = FILE_PATH+"/"+filename;
}

//   计数器位数设置函数   
public void setCounterSize(int csize) {
   counterSize = csize;
}

//   数据读取函数   
public void read() {

   try {
    counterIn = new FileInputStream(counterFileName);
    dataRead = new DataInputStream(counterIn);
    cs = dataRead.readLine();
    ci = Integer.parseInt(cs);
   } catch (Exception ex) {
    System.out.println(ex.getMessage());
   }

}

//   访问信息,访问计数记录函数   
public void write() {

   try {
    counterOut = new FileOutputStream(counterFileName);
    //infoOut = new FileOutputStream(logFileName, true);

    tempInt = ci + 1;
    buffer = (new String().valueOf(tempInt)).getBytes();
    counterOut.write(buffer);

    //tempStr = userAgent + "|" + userIP + "|" + userTime + "\r";
    //buffer = tempStr.getBytes();
    //infoOut.write(buffer);
   } catch (Exception ex) {
    System.out.println(ex.getMessage());
   }

}

//   HTML代码返回函数   
public String getHTMLCode() {

//   if (uAgent == null)
//    userAgent = "empty";
//   else
//    userAgent = uAgent;
//   if (uIP == null)
//    userIP = "empty";
//   else
//    userIP = uIP;
  
   if(!existsFile()){
    System.out.println("not found file '"+counterFileName+"'");
    write();
   }
   read();
   write();
   close();

   if (cs.length() < counterSize) { //根据计数器长度,不足则前面加零;   
    for (int i = 0; i < (counterSize - cs.length()); i++) {
     HTMLCode = HTMLCode + "<img   src='images/0.gif'>";
    }
   }

   for (int i = 0; i < cs.length(); i++) { //将记录数转换成HTML代码   
    HTMLCode = HTMLCode + "<img   src='images/";
    HTMLCode = HTMLCode + cs.charAt(i) + ".gif'>";
   }

   return HTMLCode;
}

//判断是否存在计数文件
private boolean existsFile() {
   try{
            File dirFile = new File(counterFileName);
            if(dirFile.exists()){
                return true;
            }
            
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

   return false;
}

//   文件关联关闭函数   
public void close() {

   try {
    //infoOut.close();
    counterOut.close();
    counterIn.close();
    dataRead.close();
   } catch (Exception ex) {
    System.out.println(ex.getMessage());
   }

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值