java - 从 href-attribute 获取 HTTP URL(链接的所有可能方式)
问题描述
我正在尝试做一个简单的脚本,它将任何可能的 HTML 链接转换为 HTTP URL,例如
http://example.com //example.com /index.html ./index.html index.html
我已经尝试过在另一个答案中找到的功能:
public static Integer isAbsoluteURL (String url) throws java.net.MalformedURLException {
final URL baseHTTP = new URL("http://example.com");
final URL baseFILE = new URL("file:///");
if (url.length() > 0) {
if (url.substring(0, 1) == "/") {
return 0;
}
}
URL frelative;
URL hrelative;
try {
frelative = new URL(baseFILE, url);
hrelative = new URL(baseHTTP, url);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException found");
return 3;
}
if (frelative.equals(hrelative)) {
return 0;
} else {
return 1;
}
}
我想获得绝对链接,但代码不适用于 ./, //(没有 http[s])。
谢谢。
解决方案
我更喜欢使用 spring 的 UriComponentBuilder 来处理所有事情:
https://www.baeldung.com/spring-uricomponentsbuilder
推荐阅读
- r - 使用 R:三路非参数方差分析等效?类似于 Kruskal-Wallis 但对于 3?
- push-notification - 除非手动打开应用程序,否则不会收到推送通知是真的吗?
- r - 应用函数产生错误的答案
- julia - Julia:如何配置 IJulia 内核以使用特定环境?
- java - 自定义 Arquillian ArquillianResteasyResource 应用程序路径 - 此 URL 不支持错误 HTTP 方法 POST
- python - 在这些情况下如何使用破坏
- python - 如何使用 Python 中的正则表达式在单个字符串中分隔姓氏(大写)和名字(小写)?
- css - Firefox 在绝对定位的 div 中溢出-y
- javascript - 为什么我的脚本不能在 AJAX 刷新时运行?
- regex - 免费代码营中级算法脚本:Pig Latin Regex 解决方案