比较 url 个字符串的路径。忽略域名

Compare path of url strings. Ignoring domain names

在 Java 中,我想比较两个基本上是 url 的字符串,因此如果端点相同而域不同,则应该 return 为真。 例如:https://www.example.org/dl/pi/1 should be a valid match with http://1.1.1.1/dl/pi/1

你可以这样做:

String url1 = new URL("http://1.1.1.1/dl/pi/1").getPath(); // returns /dl/pi/1
String url2 = new URL("https://www.example.org/dl/pi/1").getPath(); // returns /dl/pi/1

System.out.println(url1.equals(url2));

如果 URL 不正确,它会抛出 MalformedURLException

可以找到 Javadoc here | URL.