springboot修改address为linux的etho地址

springboot修改address为linux的etho地址,第1张

springboot修改address为linux的etho地址
@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;
    }

    }

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5707992.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-18
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存