【web】项目中如何获取ip地址和端口号
2023-09-11 14:21:26 时间
<pre name="code" class="java">/**
* 获取本地IP地址
* @return
* @throws UnknownHostException
*/
public static String getHostAddress() throws UnknownHostException {
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
InetAddress ip = ips.nextElement();
if (ip.isSiteLocalAddress()) {
return ip.getHostAddress();
}
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return InetAddress.getLocalHost().getHostAddress();
}
/**
* 获取服务端口号
* @return 端口号
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
private static String getServerPort(boolean secure) throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
MBeanServer mBeanServer = null;
if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
}
if (mBeanServer == null) {
logger.debug("调用findMBeanServer查询到的结果为null");
return "";
}
Set<ObjectName> names = null;
try {
names = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
} catch (Exception e) {
return "";
}
Iterator<ObjectName> it = names.iterator();
ObjectName oname = null;
while (it.hasNext()) {
oname = (ObjectName)it.next();
String protocol = (String)mBeanServer.getAttribute(oname, "protocol");
String scheme = (String)mBeanServer.getAttribute(oname, "scheme");
Boolean secureValue = (Boolean)mBeanServer.getAttribute(oname, "secure");
Boolean SSLEnabled = (Boolean)mBeanServer.getAttribute(oname, "SSLEnabled");
if (SSLEnabled != null && SSLEnabled) {// tomcat6开始用SSLEnabled
secureValue = true;// SSLEnabled=true但secure未配置的情况
scheme = "https";
}
if (protocol != null && ("HTTP/1.1".equals(protocol) || protocol.contains("http"))) {
if (secure && "https".equals(scheme) && secureValue) {
return ((Integer)mBeanServer.getAttribute(oname, "port")).toString();
} else if (!secure && !"https".equals(scheme) && !secureValue) {
return ((Integer)mBeanServer.getAttribute(oname, "port")).toString();
}
}
}
return "";
}
相关文章
- android开源项目下载地址
- 【Unity3D】 Unity Chan项目分享
- vue项目启动时将localhost替换成指定ip地址
- vue.js项目在nginx上部署:使spring后端记录真实ip地址
- 项目管理(一)- 启动项目
- vue.js项目在nginx上部署:使spring后端记录真实ip地址
- Docker多阶段镜像构建Dockerfile脚本示例:构建nodejs前端项目
- C++项目链接出错, error LNK2019: 无法解析的外部符号 __imp_xxxx_Allocate,该符号在函数 "xxxx" (xxxx) 中被引用
- Atitit 2016 技术趋势与没落技术 目录 1.1. 离线优先Web应用程序(Offline first web applications)1 1.2. 依照产品而不是项目(product
- Java项目练习:后台管理系统——管理员管理模块(附源码下载地址)
- Python实现GWO智能灰狼优化算法优化支持向量机回归模型(svr算法)项目实战
- 寒城攻略:Listo 教你用 Swift 写IOS UI 项目计算器
- [手游项目3]-1-入职第一天
- 互联网项目中mysql推荐(读已提交RC)的事务隔离级别
- 抖音口红机抖抖赢口红项目部署及运营记事
- 什么是项目和项目管理?
- 使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享
- 向Visual Studio Code导入ST项目
- RK3399平台开发系列讲解(项目实践篇)1.14、车机系统介绍
- 解决vue-cli项目打包出现空白页和路径错误的问题
- 【项目实战】CSS入门介绍