例如使用使用http://www.test.com访问时, request.getServerName() 会返回www.test.com 这就是域名访问
http://59.129.33.33时,会返回59.129.33.33,这就是地址访问了
主要看request.getServerName()这个方法的返回值就可以判断了、
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)
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)