统计系统的登录信息 并写入到文件中

//统计登录系统的信息  这里主要是时间   用户名   ip地址

String ip=ServletActionContext.getRequest().getRemoteAddr();
Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
String nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
String sysPath = System.getProperty("user.dir");   //可以获取到tomcat到 /bin目录
String tomcatPath=sysPath.replace("bin","logs");    //修改路径到tomcat下的/logs/
System.out.println(loginUser.getLoginId()+"=="+ip+"=="+nowTime+"=="+sysPath);
String longinMessage=nowTime+"               "+loginUser.getLoginId()+"               "+ip;
FileWriter  fileWrite = null;
try {
 fileWrite=new FileWriter(tomcatPath+File.separator+"loginMessage.log",true);  //true  表示追加日志
 fileWrite.write(longinMessage);
 fileWrite.write("\r\n");    //换行
 fileWrite.flush();    
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileWrite.close();
} catch (IOException e) {
e.printStackTrace();
}
}
需要主要的是:  String ip=ServletActionContext.getRequest().getRemoteAddr();   如何得到的结果是  0:0:0:0:0:0:0:1,可以把访问地址: localhost:8080修改为127.0.0.1:8080;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值