java判断输入的字符串是否一个域名。
源代码为:packagecom.phy.tools;importjava.io.IOException;importjava.net.InetAddress;import...
源代码为:
package com.phy.tools;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* PingDomain.java
*
* @author 判断输入的域名是否有效
*/
public class PingDomain {
public void isDomain(String addressArr) throws IOException {
boolean flag=false;
InetAddress address = InetAddress.getByName(addressArr);
if(address.isReachable(30)){
flag=true;
}
System.out.println("是一个域名吗?"+flag);
System.out.println("---------------------------------------");
}
public static void main(String[] args) throws Exception {
PingDomain pmd = new PingDomain();
pmd.isDomain("111.a.htm");
}
}
运行时抛出:Exception in thread "main" java.net.UnknownHostException: 111.a.htm
这样的错误。
求解。 展开
package com.phy.tools;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* PingDomain.java
*
* @author 判断输入的域名是否有效
*/
public class PingDomain {
public void isDomain(String addressArr) throws IOException {
boolean flag=false;
InetAddress address = InetAddress.getByName(addressArr);
if(address.isReachable(30)){
flag=true;
}
System.out.println("是一个域名吗?"+flag);
System.out.println("---------------------------------------");
}
public static void main(String[] args) throws Exception {
PingDomain pmd = new PingDomain();
pmd.isDomain("111.a.htm");
}
}
运行时抛出:Exception in thread "main" java.net.UnknownHostException: 111.a.htm
这样的错误。
求解。 展开
3个回答
展开全部
address.isReachable(30),这个不好使,ping得通这个函数也可能返回false
根据你的目标平台,修改"bytes from"即可
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* PingDomain.java
*
* @author 判断输入的域名是否有效
*/
public class PingDomain {
public void isDomain(String addressArr) throws IOException {
boolean flag=false;
InetAddress address = null;
try {
address = InetAddress.getByName(addressArr);
System.out.println("address = " + address);
} catch (UnknownHostException e) {
System.out.println("输入非法!");
return;
}
System.out.println("是一个域名吗?"+isReachable(address.getHostAddress()));
System.out.println("---------------------------------------");
}
private boolean isReachable(String ip) {
Runtime r = Runtime.getRuntime();
int timeout = 2;
String pingCommand = "ping " + ip + " -w " + timeout;
BufferedReader in = null;
try {
Process p = r.exec(pingCommand);
if (p == null) {
System.out.println("Failed.");
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ( (line = in.readLine()) != null) {
System.out.println(line);
if (line.contains("bytes from")) {
System.out.println("Conected.");
p.destroy();
return true;
}
}
} catch (Exception ex) {
System.out.println("Failed.");
} finally {
try {
in.close();
} catch (Exception ex) {
}
}
return false;
}
public static void main(String[] args) throws Exception {
PingDomain pmd = new PingDomain();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String inputline=in.readLine();
pmd.isDomain(inputline);
}
}
根据你的目标平台,修改"bytes from"即可
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* PingDomain.java
*
* @author 判断输入的域名是否有效
*/
public class PingDomain {
public void isDomain(String addressArr) throws IOException {
boolean flag=false;
InetAddress address = null;
try {
address = InetAddress.getByName(addressArr);
System.out.println("address = " + address);
} catch (UnknownHostException e) {
System.out.println("输入非法!");
return;
}
System.out.println("是一个域名吗?"+isReachable(address.getHostAddress()));
System.out.println("---------------------------------------");
}
private boolean isReachable(String ip) {
Runtime r = Runtime.getRuntime();
int timeout = 2;
String pingCommand = "ping " + ip + " -w " + timeout;
BufferedReader in = null;
try {
Process p = r.exec(pingCommand);
if (p == null) {
System.out.println("Failed.");
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ( (line = in.readLine()) != null) {
System.out.println(line);
if (line.contains("bytes from")) {
System.out.println("Conected.");
p.destroy();
return true;
}
}
} catch (Exception ex) {
System.out.println("Failed.");
} finally {
try {
in.close();
} catch (Exception ex) {
}
}
return false;
}
public static void main(String[] args) throws Exception {
PingDomain pmd = new PingDomain();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String inputline=in.readLine();
pmd.isDomain(inputline);
}
}
展开全部
InetAddress.getByName()这个是通过计算机名字来得到的地址,根本不是你所要求的判断域名是否有效,最终只是判断计算机名是否存在,而且你设的时间是isReachable(30)中30太小了,设置大点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void isDomain(String addressArr) throws IOException 把这句的IOException改成Exception就行了啊! 因为InetAddress address = InetAddress.getByName(addressArr);貌似也要抛出异常并且不是IOException
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |