獲取ip和端口號


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();
  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM