0
点赞
收藏
分享

微信扫一扫

java 域名转换

westfallon 2023-04-20 阅读 59

import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class IpNameConvert {
 
    InetAddress myIPaddress = null;
 
    InetAddress myServer = null;
 
    public static void main(String args[]) {
    IpNameConvert mytool;
        mytool = new IpNameConvert();
        System.out.println("Your host IP is: " + mytool.getMyIP());
        System.out.println("The Server IP is :" + mytool.getServerIP());
 
    }
 
    // 取得LOCALHOST的IP地址
    public InetAddress getMyIP() {
        try {
            myIPaddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
        }
        return (myIPaddress);
    }
 
    // 取得 www.abc.com 的IP地址
    public InetAddress getServerIP() {
        try {
            myServer = InetAddress.getByName("www.baidu.com");
        } catch (UnknownHostException e) {
        }
        return (myServer);
    }
 
}
 
 
            System.out.println(myServer.getHostName());
            System.out.println(myServer.getHostAddress());

举报

相关推荐

0 条评论