Java怎么判断URL是域名格式还是IP加端口格式?

Java怎么判断URL是域名格式还是IP加端口格式?,第1张

判断方法:outTODOAuto-generatedcatchblocke,那就是说当做的域名,如果解析出来的IP和域名字符串相等.printStackTrace()}catch(MalformedURLExceptione){/简单的办法是判断host格式.cn"target="_blank">http/://www,其实是ip.out.com,复杂的但稳妥的办法是当做域名处理.getByName(host))://wwwelseSystemTODOAuto-generatedcatchblockeInetAddressaddress=null/.cn")if(host.getHostAddress()))Systemdomain".println(".printStackTrace()).println(".equalsIgnoreCase(addressaddress=InetAddress,域名解析ip".sina.getHost()}catch(UnknownHostExceptione){/.com.sina。try{URLurl=newURL("Stringhost=url

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)

}

}


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/808520.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-08-29
下一篇2023-08-29

发表评论

登录后才能评论

评论列表(0条)

    保存