
正文
java代码获取访问地址 java获取访问用户的mac地址
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA如何获得外网IP地址?
java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
EnumerationNetworkInterface netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() !finded) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}
相关问答
Q1: java如何获取浏览器的访问网址及其内容
通过request俩获取,以下是request的方法介绍:
getParameterNames:取得客户端所发出的请求参数名称.
getParameter:可以让您指定请求参数名称,以取得对应的设定值.
getServerName:请求的服务器.
getProtocol:使用协议.
getMethod:请求方法.
getServerPort:请求端口号.
getContextPath:Context路径.
getServletPath: Servlet路径.
getRequestURI:URI路径.
getQueryString:查询字符串.
getRemoteAddr:使用者主机IP.
getRemotePort:使用者使用端口号.追问
我的意思是:例如,你正在上网,然后我通过这个就可以知道你上的什么网站,什么内容。
还有上边的如果行。
请问它在那个包里?
Q2: java 中如果获取来访路径?
Javascript 中
1.获得上一个页面的URL地址
var url = document.referrer;
2.获得当前页面的URL地址
var ur = location.href ;
java 中
1.获得上一个页面的URL地址
Request.getHeader("Referer");
2 获得当次请求的URI请求相对路径和请求参数
request.getRequestURI()+"?"+request.getQueryString();
Q3: JAVA怎么获取IP地址
这个是获取不到的,因为有代理、端口映射等等转发情况的存在。为什么不保存相对路径/域名/或者在服务器上某个配置文件中配置域名/数据库中一个表/数据库中某个字段保存当前服务器的ip地址呢?
java代码获取访问地址的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取访问用户的mac地址、java代码获取访问地址的信息别忘了在本站进行查找喔。






