时间:2021-05-19
第一种直接用java自带的InetAddress类:
import java.net.InetAddress;import java.text.SimpleDateFormat;import java.util.Date; public class test{ public static void main(String[] args) throws Exception{ InetAddress addr = InetAddress.getLocalHost(); String ip=addr.getHostAddress();//获得本机IP String address=addr.getHostName();//获得本机名称 Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateNowStr = sdf.format(d); System.out.println("IP: "+ip+" Name: "+address+" Time: "+dateNowStr); } }输出结果如下:
第二种根据Web request请求获取:
package demo.login; import ins.framework.web.Struts2Action;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpServletRequest;import java.net.InetAddress; @SuppressWarnings("serial")public class LoginAction extends Struts2Action { public String login() { HttpServletRequest request=this.getRequest(); this.getIpAddress(request); return SUCCESS; } public void getIpAddress(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); System.out.println("x-forwarded-for ip: " + ip); if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { // 多次反向代理后会有多个ip值,第一个ip才是真实ip if( ip.indexOf(",")!=-1 ){ ip = ip.split(",")[0]; } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); System.out.println("Proxy-Client-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); System.out.println("WL-Proxy-Client-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); System.out.println("HTTP_CLIENT_IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); System.out.println("X-Real-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); System.out.println("getRemoteAddr ip: " + ip); } //获取本地ip if("0:0:0:0:0:0:0:1".equals(ip)){ try { ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("getLocal ip: " + ip); } Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateNowStr = sdf.format(d); System.out.println("登录IP: " + ip+"登录时间:"+dateNowStr); } }本地会输出:
服务器端如下:
到此这篇关于java获取登录者IP和登录时间的两种实现代码详解的文章就介绍到这了,更多相关java获取登录者IP和登录时间内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文以C与MFC的两个实例详述了取外网IP的两种实现方法,具体实现代码如下:MFC语言实现获取外网IP:#include#include#pragmacomme
本文实例介绍了javascript动态获取登录时间和在线时长的相应代码,分享给大家供大家参考,具体内容如下效果图:实现代码:online///这里是获得登录时候
本文实例讲述了PHP实现获取ip地址的5种方法,以及插入用户登录日志操作。分享给大家供大家参考,具体如下:php获取ip地址的5种方法,插入用户登录日志实例,推
Java两种延时thread和timer详解及实例代码在Java中有时候需要使程序暂停一点时间,称为延时。普通延时用Thread.sleep(int)方法,这很
Java获取文件大小今天写代码时需要实现获取文件大小的功能,目前有两种实现方法,一种是使用File的length()方法;另外一种是使用FileInputStr