首页 > 其他 > 详细

获取ip和端口号

时间:2019-11-01 22:34:32      阅读:131      评论:0      收藏:0      [点我收藏+]

package com.sf.confirmation.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.water.common.exception.SysException;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author 80004084
* @date 2019/9/30 15:18
* @description :获取ip和端口号
*/
@Component
public class InternetInfo implements ApplicationListener<WebServerInitializedEvent> {

  private final static Logger LOGGER = LoggerFactory.getLogger(InternetInfo.class);

  private int serverPort;

  @Override
  public void onApplicationEvent(WebServerInitializedEvent event) {  
    this.serverPort = event.getWebServer().getPort();
  }

  public int getPort() {
    return this.serverPort;
  }

  public String getLocalHostAddress() {
    InetAddress address = null;
  try {
    address = InetAddress.getLocalHost();
  } catch (UnknownHostException e) {
    LOGGER.error("UnknownHostException:",e);
    throw new SysException("获取服务器ip和端口失败");
  }
  return address.getHostAddress() + ":" + getPort();
  }

}

获取ip和端口号

原文:https://www.cnblogs.com/pxzbky/p/11779638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!