如何比较java中的两个URL? [英] How to compare two URLs in java?

查看:117
本文介绍了如何比较java中的两个URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题 - 给定两个url,是否有一些内置方法,或者一个Apache库来决定它们(逻辑上)是否相等?

Here's a simple problem - given two urls, is there some built-in method, or an Apache library that decides whether they are (logically) equal?

For例如,这两个网址相同:

For example, these two urls are equal:

http://stackoverflow.com
http://stackoverflow.com/


推荐答案

URI.equals() (以及有问题的 URL.equals() 返回 true 对于这些特定示例,我认为这是唯一可以假设等价的情况(因为HTTP协议中没有空路径)。

While URI.equals() (as well as the problematic URL.equals()) does not return true for these specific examples, I think it's the only case where equivalence can be assumed (because there is no empty path in the HTTP protocol).

URI http://stackoverflow.com/foo http://stackoverflow.com/foo/ 可以假设是等效的。

The URIs http://stackoverflow.com/foo and http://stackoverflow.com/foo/ can not be assumed to be equivalent.

也许你可以使用 URI.equals()包装在一个显式处理这个特定情况的实用程序方法中。

Maybe you can use URI.equals() wrapped in a utility method that handles this specific case explicitly.

这篇关于如何比较java中的两个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆