码迷,mamicode.com
首页 > 编程语言 > 详细

Java 查询URL对应IP地址

时间:2014-09-26 11:20:28      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   java   ar   for   div   2014   on   

/**
 * @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地址

标签:blog   io   os   java   ar   for   div   2014   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!