java如何获取当前登录ip
展开全部
为适应不同的网络情况,提供这个类:看下面的代码吧,Copy过去就能用。
import javax.servlet.http.HttpServletRequest;
public class HttpUtil {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
import javax.servlet.http.HttpServletRequest;
public class HttpUtil {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家国内优质海外HTTP代理商,拥有一个庞大的IP资源池,覆盖200多个地区,IP数量大且匿名度高。其优点还包括超高并发、稳定高效、技术服务等特点,同时提供HTTP、HTTPS以及SOCKS5协议支持。此外,Sto...
点击进入详情页
本回答由Storm代理提供
2016-07-26 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
第一种:获取本机的IP
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:" + ips.nextElement().getHostAddress());
ipTemp= ni.getInetAddresses().nextElement().getHostAddress();
if(ipTemp!="127.0.0.1" && !"127.0.0.1".equals(ipTemp))
{
ip=ipTemp;
}
}
}
}catch(Exception ee)
{
ee.printStackTrace();
}
第二种:也是本机的:
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString();//获得本机IP
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:" + ips.nextElement().getHostAddress());
ipTemp= ni.getInetAddresses().nextElement().getHostAddress();
if(ipTemp!="127.0.0.1" && !"127.0.0.1".equals(ipTemp))
{
ip=ipTemp;
}
}
}
}catch(Exception ee)
{
ee.printStackTrace();
}
第二种:也是本机的:
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString();//获得本机IP
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
request.getRemoteAddr();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |