如何在 Java 中获取父 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10159186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:52:01  来源:igfitidea点击:

How to get parent URL in Java?

javaurl

提问by Eonil

In Objective-C I use -[NSURL URLByDeletingLastPathComponent]to get parent URL. What's the equivalent of this in Java?

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

回答by ?eurobur?

Shortest snippet of code I can think of is this:

我能想到的最短代码片段是这样的:

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

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

回答by ulmangt

I don't know of library function to do this in one step. However, the following (admittedly cumbersome) bit of code I believe accomplishes what you're after (and you could wrap this up in your own utility function):

我不知道可以一步完成此操作的库函数。但是,我相信以下(无可否认的繁琐)代码可以完成您所追求的(并且您可以将其包含在您自己的实用程序函数中):

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 );
    }
}

回答by Tomasz Szymulewski

Here is very simple solution which was the best approach in my use case:

这是非常简单的解决方案,这是我用例中的最佳方法:

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

I created simple function, I was inspired by the code of File::getParent. In my code there is no issue with back slashes on Windows. I assume that resourcePathis resource part of URL, without protocol, domain and port number. (e.g. /articles/sport/atricle_nr_1234)

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

回答by Karan Arora

The simple solution offered by Guava library.

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

Code:

代码:

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);

Output:

输出:

Immediate ancestor: ibm.watson.co.uk

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

Top private domain - watson.co.uk

For reference: https://guava.dev/releases/snapshot/api/docs/com/google/common/net/InternetDomainName.html

供参考:https: //guava.dev/releases/snapshot/api/docs/com/google/common/net/InternetDomainName.html

Dependency required:

需要依赖:

https://mvnrepository.com/artifact/com.google.guava/guava

https://mvnrepository.com/artifact/com.google.guava/guava

I'm using version 19.0

我使用的是 19.0 版

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

And, many more related functionalities are provided by this class InternetDomainName.

并且,此类InternetDomainName提供了更多相关功能。