用Java获取本机IP或者请求用户的真正IP地址
杭州第二课堂
⽤Java获取本机IP或者请求⽤户的真正IP地址
⼀、在Web请求中获取请求⽤户的IP地址
publicstaticString getUserRealIP(HttpServletRequest request)throws UnknownHostException {
String ip = "";
// 有的user可能使⽤代理,为处理⽤户使⽤代理的情况,使⽤Header("x-forwarded-for") ==null)  {        ip = RemoteAddr();
}  else  {
ip = Header("x-forwarded-for");
}
if("127.0.0.1".equals(ip))  {
用高情商表达我想你// 获取本机真正的ip地址        ip = LocalHost().getHostAddress();
}
return ip;
}
⼆、获取本机IP地址和主机名
importjava.util.*;
importjava.*;publicclass getMyIP
桌面图标变了{
publicstaticvoid main(String[] args) {
String ip =null;
String host =null;
try{
InetAddress ia = LocalHost();
猪肉丸子
host = ia.getHostName();//获取计算机名字            ip = ia.getHostAddress();//获取IP        }
catch(UnknownHostException e) {
e.printStackTrace();
}
System.out.println(host);
System.out.println(IP);
}
}
三、⽹上搜索的⽅法
import javax.servlet.http.HttpServletRequest;/** * ⾃定义访问对象⼯具类
*
* 获取对象的IP地址等信息
* @author X-rapido
*
*/publicclass CusAccessObjectUtil {
/**    * 获取⽤户真实IP地址,不使⽤RemoteAddr();的原因是有可能⽤户使⽤了代理软件⽅式避免真实IP地址,
党员自评简短50字*
* 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不⽌⼀个,⽽是⼀串IP值,究竟哪个才是真正的⽤户端的真实IP呢?    * 答案是取X-Forwarded-For中第⼀个⾮unknown的有效IP字符串。
*
* 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
* 192.168.1.100
*
* ⽤户真实IP为: 192.168.1.110
*
* @param request
* @return*/publicstatic String getIpAddress(HttpServletRequest request) {
String ip = Header("x-forwarded-for");
if(ip ==null|| ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("Proxy-Client-IP");
}
if(ip ==null|| ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("WL-Proxy-Client-IP");
}
if(ip ==null|| ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("HTTP_CLIENT_IP");
}
if(ip ==null|| ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("HTTP_X_FORWARDED_FOR");
}
if(ip ==null|| ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
无法获取ip地址
ip = RemoteAddr();
if("127.0.0.1".equals(ip)||"0:0:0:0:0:0:0:1".equals(ip)){
//根据⽹卡取本机配置的IPInetAddress inet=null;
try {
inet = LocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ip= HostAddress();
}
}
return ip;
}
}
四、扩展知识
Java获取客户端请求电脑IP,操作系统等等各种信息
HttpServletRequest request = Request();
System.out.println("浏览器基本信息:"+Header("user-agent"));        System.out.println("客户端系统名称:"+Property("os.name"));
System.out.println("客户端系统版本:"+Property("os.version"));        System.out.println("客户端操作系统位数:"+Property("os.arch"));        System.out.println("HTTP协议版本:"+Protocol());
System.out.println("请求编码格式:"+CharacterEncoding());
System.out.println("Accept:"+Header("Accept"));
System.out.println("Accept-语⾔:"+Header("Accept-Language"));        System.out.println("Accept-编码:"+Header("Accept-Encoding"));        System.out.println("Connection:"+Header("Connection"));
System.out.println("Cookie:"+Header("Cookie"));
System.out.println("客户端发出请求时的完整URL"+RequestURL());        System.out.println("请求⾏中的资源名部分"+RequestURI());
System.out.println("请求⾏中的参数部分"+RemoteAddr());
System.out.println("客户机所使⽤的⽹络端⼝号"+RemotePort());
System.out.println("WEB服务器的IP地址"+LocalAddr());
System.out.println("WEB服务器的主机名"+LocalName());
System.out.println("客户机请求⽅式"+Method());
System.out.println("请求的⽂件的路径"+ServerName());
System.out.println("请求体的数据流"+Reader());
BufferedReader Reader();
String res = "";
while((res = br.readLine()) !=null) {
System.out.println("request body:" + res);
}
System.out.println("请求所使⽤的协议名称"+Protocol());
System.out.println("请求中所有参数的名字"+ParameterNames());
Enumeration enumNames= ParameterNames();
while (enumNames.hasMoreElements()) {
String key = (String) Element();
System.out.println("参数名称:"+key);
}
五、在本机上获取计算机信息
publicvoid getIP(){
try {
InetAddress address = LocalHost();
String ip = HostAddress().toString();//获取本机IPString hostName = HostName().toString();//获取本机计算机名称System.out.println("ip is:"+ip);
System.out.println("host name is:"+hostName);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
个⼈博客

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。