Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理在HTML页面中找到的URL_Java_Html_Url - Fatal编程技术网

Java 处理在HTML页面中找到的URL

Java 处理在HTML页面中找到的URL,java,html,url,Java,Html,Url,我有一个HTML页面。我使用jsoup解析它,下面是代码的一部分 Document doc = Jsoup.parse(content); org.jsoup.select.Elements images = doc.select("[src]"); for(org.jsoup.nodes.Element img : images) { // Here I need to determine the type of the URL and convert it to absolute

我有一个
HTML
页面。我使用
jsoup
解析它,下面是代码的一部分

Document doc = Jsoup.parse(content);

org.jsoup.select.Elements images = doc.select("[src]");

for(org.jsoup.nodes.Element img : images) {
    // Here I need to determine the type of the URL and convert it to absolute URL
}
我需要将此HTML中的所有URL更改为绝对URL。 问题是,
src
属性可能是类型,如果主机if
www.example.com

 1. http://www.example.com/images/1.png
 2. http://example.com/images/1.png
 3. www.example.com/images/1.png
 4. example.com/images/1.png
 5. /example.com/images/1.png
 6. //example.com/images/1.png
 7. /images/1.png
我在测试时提出了这个列表,所以我应该支持它们。所以我需要一些函数来输出绝对URL(
http://www.example.com/images/1.png
)用于上面列出的所有输入。 当URL用于另一个位置的资源时,问题甚至变得复杂,例如
haha.com/images/1.png

所以我需要一种方法来确定URL的类型,比如:

  • 相对(
    /images/1.png
  • 绝对(
    http://example.com/images/1.png
  • 协议相关(
    example.com/images/1.png

用java解决这个问题的最佳方法是什么?谢谢。

查看DOM中可用的方法。特别是:document.URL