繁体   English   中英

如何从Java中的绝对url中提取相对url

[英]How to extract the relative url from the absolute url in Java

我有这个网站:

https://asd.com/somestuff/another.html

我想从中提取相关部分:

somestuff/another.html

我怎么做?

编辑:我得到了一个问题的答案,但问题是从亲戚中构建绝对值 url,这不是我感兴趣的。

您可以使用URL对象的getPath()方法:

URL url = new URL("https://asd.com/somestuff/another.html");
System.out.println(url.getPath());  // prints "/somestuff/another.html"

现在,这只会带来实际路径。 如果需要更多信息(锚点或作为获取值传递的参数),则需要调用URL对象的其他访问器:

URL url = new URL("https://asd.com/somestuff/another.html?param=value#anchor");
System.out.println(url.getPath());  // prints "/somestuff/another.html"
System.out.println(url.getQuery()); // prints "param=value"
System.out.println(url.getRef());   // prints "anchor"

根据Hiru 的回答,无需太多代码即可生成相对 URL:

URL absolute = new URL(url, "/");
String relative = url.toString().substring(absolute.toString().length());
System.out.println(relative); // prints "somestuff/another.html?param=value#anchor"

如果您知道域将始终是 .com,那么您可以尝试以下操作:

String url = "https://asd.com/somestuff/another.html";
String[] parts = url.split(".com/");
//parts[1] is the string after the .com/

URL 由以下元素组成(注意一些可选元素被省略): 1) 方案 2) 主机名 3) [端口] 4) 路径 5) 查询 6) 片段 使用Java URL API,您可以执行以下操作:

URL u = new URL("https://randomsite.org/another/randomPage.html");
System.out.println(u.getPath());

编辑#1 看到 Chop 的回答,以防您的 URL 中有查询元素,例如

?name=foo&value=bar

使用getQuery()方法不会返回资源路径,只会返回查询部分。

尝试这个

在全球范围内使用它,不仅适用于 .com

    URL u=new URL("https://asd.in/somestuff/another.html");
    String u1=new URL(u, "/").toString();
    String u2=u.toString();
    String[] u3=u2.split(u1);
    System.out.println(u3[1]); //it prints:   somestuff/another.html

您可以使用以下代码段执行此操作。

 String str="https://asd.org/somestuff/another.html";
    if(str.contains("//")) //To remove any protocol specific header.
    {
        str=str.split("//")[1];
    }
    System.out.println(str.substring(str.indexOf("/")+1)); // taking the first '/'

我的基于java.net.URI解决方案

URI _absoluteURL = new URI(absoluteUrl).normalize();
String root = _absoluteURL.getScheme() + "://" + _absoluteURL.getAuthority();
URI relative = new URI(root).relativize(_absoluteURL);

String result = relative.toString();

考虑使用 Apache Commons VFS...

import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLStreamHandlerFactory;

public class StudyURI {
    public static void main(String[] args) throws URISyntaxException, FileSystemException {
        StandardFileSystemManager fileSystemManager = (StandardFileSystemManager) VFS.getManager();
        URLStreamHandlerFactory factory = fileSystemManager.getURLStreamHandlerFactory();
        URL.setURLStreamHandlerFactory(factory);

        URI baseURI = fileSystemManager.resolveFile("https://asd.com/").getURI();
        URI anotherURI =fileSystemManager.resolveFile("https://asd.com/somestuff/another.html").getURI();

        String result = baseURI.relativize(anotherURI).getPath();

        System.out.println(result);
    }
}

也许您需要添加模块来运行代码:
https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM