
@SpringBootApplication
public class YibutaskApplication {
public static void main(String[] args) {
SpringApplication.run(YibutaskApplication.class, args);
}
Logger logger =Logger.getLogger(YibutaskApplication.class);
@Bean
public TomcatServletWebServerFactory servletWebServerFactory(){
// String address = "localhost";
// Integer port = 12345;
String address=getIP();
System.out.println(address);
TomcatServletWebServerFactory container = new TomcatServletWebServerFactory();
try {
container.setAddress(InetAddress.getByName(address));
return container;
} catch (UnknownHostException e) {
logger.error("can not resolve host:" + address, e);
throw new RuntimeException("can not resolve host:" + address, e);
}
}
private String getIP(){
String address = "" ;
String os = System.getProperty( "os.name" );
System.out.println(os);
if (os.startsWith( "Linux" )){
try {
Process p = Runtime.getRuntime().exec("ifconfig eth0");
BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null ){
int startIndex = line.indexOf( "inet" );
int endIndex = line.indexOf( "netmask" );
if (startIndex !=- 1 ){
address = line.substring(startIndex + 4,endIndex );
break ;
}
}
br.close();
return address.trim();
} catch (IOException ex) {
logger.error( "", ex);
}
}
return address;
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)