首页 > 编程语言 > 详细

Java 查询URL对应IP地址

时间:2014-09-26 11:01:39      阅读:276      评论:0      收藏:0      [点我收藏+]
/**
 * @ClassName TestSocket1
 * @Version 1.0
 * @Date 2014-9-26 上午10:19:36
 */
public class TestSocket1 {

	public static void main(String[] args) throws UnknownHostException {

		String str = "";
		Scanner scanner = new Scanner(System.in);

		while (true) {
			System.out.println("请输入需要查询的URL:");
			str = scanner.nextLine().trim();
			if (str.equalsIgnoreCase("exit")) {
				System.out.println("程序退出");
				break;
			} else if (str.length() == 0) {
				System.out.println("本机IP " + getLocalHost(str));
			} else {
				System.out.println(Arrays.toString(getWebHost(str).toArray()));
			}
		}
	}

	private static String getLocalHost(String url) throws UnknownHostException {
		return InetAddress.getLocalHost().toString();
	}

	private static List<String> getWebHost(String url)
			throws UnknownHostException {

		List<String> res = new LinkedList<String>();
		InetAddress[] address = InetAddress.getAllByName(url);
		for (InetAddress inetaddress : address) {
			res.add(inetaddress.getHostAddress());
		}
		return res;
	}
}

  

Java 查询URL对应IP地址

原文:http://www.cnblogs.com/zhonghuazhi/p/3994390.html

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