如何在Java中获得父URL?

zbdgwd5y  于 2023-03-06  发布在  Java
关注(0)|答案(7)|浏览(126)

在Objective-C中,我使用-[NSURL URLByDeletingLastPathComponent]来获取父URL。在Java中,它的等价物是什么?

jm81lzqq

jm81lzqq1#

我能想到的最短的代码片段如下:

URI uri = new URI("http://www.stackoverflow.com/path/to/something");

URI parent = uri.getPath().endsWith("/") ? uri.resolve("..") : uri.resolve(".");
ukqbszuj

ukqbszuj2#

我不知道有哪个库函数可以一步完成这个任务,但是,我相信下面这段代码(确实很麻烦)可以完成你所追求的目标(你可以把它 Package 在你自己的实用函数中):

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest
{
    public static void main( String[] args ) throws MalformedURLException
    {
        // make a test url
        URL url = new URL( "http://stackoverflow.com/questions/10159186/how-to-get-parent-url-in-java" );

        // represent the path portion of the URL as a file
        File file = new File( url.getPath( ) );

        // get the parent of the file
        String parentPath = file.getParent( );

        // construct a new url with the parent path
        URL parentUrl = new URL( url.getProtocol( ), url.getHost( ), url.getPort( ), parentPath );

        System.out.println( "Child: " + url );
        System.out.println( "Parent: " + parentUrl );
    }
}
ac1kyiln

ac1kyiln3#

下面是一个非常简单的解决方案,在我的使用案例中是最好的方法:

private String getParent(String resourcePath) {
    int index = resourcePath.lastIndexOf('/');
    if (index > 0) {
        return resourcePath.substring(0, index);
    }
    return "/";
}

我创建了一个简单的函数,我的灵感来自于File::getParent的代码。在我的代码中,在Windows上没有反斜杠的问题。我假设resourcePath是URL的资源部分,没有协议,域和端口号。(例如/articles/sport/atricle_nr_1234

ee7vknir

ee7vknir4#

Guava库提供的简单解决方案。

代码:

URL url = new URL("https://www.ibm.watson.co.uk");
String host = url.getHost();

InternetDomainName parent = InternetDomainName.from(host).parent(); // returns ibm.watson.co.uk
System.out.println("Immediate ancestor: "+parent);

ImmutableList<String> parts = InternetDomainName.from(host).parts(); 
System.out.println("Individual components:  "+parts);

InternetDomainName name = InternetDomainName.from(host).topPrivateDomain(); // watson.co.uk
System.out.println("Top private domain - " + name);

输出:

Immediate ancestor: ibm.watson.co.uk

Individual components:  [www, ibm, watson, co, uk]

Top private domain - watson.co.uk

供参考:https://guava.dev/releases/snapshot/api/docs/com/google/common/net/InternetDomainName.html
所需依赖关系:
https://mvnrepository.com/artifact/com.google.guava/guava
我使用的是19.0版本

<dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
</dependency>

此外,此类InternetDomainName还提供了更多相关功能。

cngwdvgl

cngwdvgl5#

更简洁的方式

import java.nio.file.Paths;
URI child=....;
URI parent=Paths.get(child).getParent().toUri();

就是这样

wr98u20j

wr98u20j6#

使用java.nio.file.paths,这可以在一行中完成。
例如:

String parent = Paths.get("https://stackoverflow.com/questions/10159186/how-to-get-parent-url-in-java/").getParent().toString();
System.out.println(parent);

将打印:

https:/stackoverflow.com/questions/10159186

请记住https:/缺少一个斜杠,因此您可以将其添加到另一个替换中:

String parent = Paths.get("https://stackoverflow.com/questions/10159186/how-to-get-parent-url-in-java/").getParent().toString().replace("https:/","https://");

System.out.println(parent);

将打印:

https://stackoverflow.com/questions/10159186
sqyvllje

sqyvllje7#

如果您有javax.ws.rs.core.UriBuilder

URI location;
UriBuilder.fromUri(location).path("..").build().normalize();

相关问题