1、如何使用Java来实现域名和IP地址的转换
域名转IP:
public static void main(String[] args) {
try{
InetAddress[] addrs=InetAddress.getAllByName("www.网络.com");
if(null!=addrs){
for(int i=0;i<addrs.length;i++){
System.out.println(addrs[i].getHostAddress());
}
}
}catch(Exception e){
}
}
2、java通过域名怎么获取本机ip
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NsLookup {
static public void main(String[] args) {
try {
System.out.println("try");
InetAddress address = InetAddress.getByName(args[0]);
System.out.println(args[0]+" : "+address.getHostAddress());
}catch(UnknownHostException uhe) {
System.out.println("catch");
System.err.println("Unable to find: "+args[0]);
}
}
}
3、java 如何禁止通过ip访问服务器,而只能通过域名访问?如果是通过ip访问,那么跳转到相应的提示页面,谢谢
禁止通过ip访问服务器:不需要编程即可实现。
如果服务器是IIS:把默认站点设置成【跳转到相应的提示页面】即可;
同时把你的域名站,用80端口的指定域名即可。
具体操作是:新建域名站--》指定端口8080等--》到站点属性的【高级】中,添加域名和端口80的指定即可。
4、Java怎么判断URL是域名格式还是IP加端口格式?
简单的办法是判断host格式,复杂的但稳妥的办法是当做域名处理,域名解析,如果解析出来的IP和域名字符串相等,那就是说当做的域名,其实是ip。
try {
URL url=new URL("http://www.sina.com.cn");
String host=url.getHost();
InetAddress address = null;
address = InetAddress.getByName(host);
if(host.equalsIgnoreCase(address.getHostAddress()))
System.out.println("ip");
else
System.out.println("domain");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5、java中怎么将域名解析为ip
可参与下述代码:
public String getIP(String name) {6、java __String类型的IP地址,怎么转换成InetAddress 型IP。。
在 java 中将 String 类型的 IP 地址转换成 InetAddress 类型IP的话需要使用 InetAddress 类所提供的 getByName() 方法进行转换.
例:
String ip = "192.168.0.1";不过这里内需要有容一点注意, InetAddress 类提供的 getByName() 参数为一个字符串,如果当这个字符串是一个非正规的 IP 地址格式的话,需要处理 UnknownHostException 异常
7、java通过域名获取IP地址
public String getIP(String name){
InetAddress address = null;
try {
address = InetAddress.getByName(name);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取失败");
}
return address.getHostAddress().toString();
}
8、java IP反查域名,我随意输入IP,能得到此IP下所有的域名,例子,思路,资料,什么都要
这个要看操作系统的,不同命令行实现。
Java调命令行的许多了,百度出去一堆回来,故不详述。
如下:
如果是Linux,则要用到一些arp指令,比如whois之类的。
如果是Windows,则要用到tracert之类的追踪指令。