简体   繁体   English

仅从Java中的IP地址获取网址

[英]getting only url from ip address in java

The following java code will convert an IP Address to its host. 以下Java代码会将IP地址转换为其主机。

InetAddress addr = InetAddress.getByName("31.13.78.13");
String host = addr.getHostName();
System.out.println(host);

Here host will be printed as "edge-star-shv-01-sit4.facebook.com". 在这里,主机将被打印为“ edge-star-shv-01-sit4.facebook.com”。 The first portion may be the address of a data center of facebook. 第一部分可以是facebook的数据中心的地址。 I only want the "facebook.com" portion. 我只想要“ facebook.com”部分。 I could not find any built in method in InetAddress library that can sort this out. 我在InetAddress库中找不到任何可以解决此问题的内置方法。 I can solve the problem using substring method but this is not a good way because I want a generalized approach. 我可以使用子字符串方法解决问题,但这不是一个好方法,因为我想要一种通用方法。 Is there any way where I can get "facebook.com" instead of "edge-star-shv-01-sit4.facebook.com" by taking "31.13.78.13" as input? 有什么办法可以通过将“ 31.13.78.13”作为输入来获取“ facebook.com”而不是“ edge-star-shv-01-sit4.facebook.com”?

You can't! 你不能!

I run multiple websites on one IP adress. 我在一个IP地址上运行多个网站。 So if you would use a script as you discribe it here, what should it return? 因此,如果您在此处描述脚本时使用脚本,它应该返回什么?

The only thing is to get the hostname of the server with the selected IP, this can contain the website name , as in your case, but it doesn't have to. 唯一的办法是获取具有所选IP的服务器的主机名,这可以包含网站名,就像您所使用的一样,但这不是必须的。 Imagine a server with hostname " example.com " and IP 1.1.1.1 , or whatever, on this machine a website in running with url "stackoverflow.com" then you expect when tracing the IP adress to get " stackoverflow.com " but in fact you would get " example.com " 想象一下,一台主机名为“ example.com ”,IP 1.1.1.1或其他版本的服务器,在此计算机上,一个以url“ stackoverflow.com”运行的网站,那么您期望在跟踪IP地址时获得“ stackoverflow.com ”,但是在事实上你会得到“ example.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM