如何获取URL的路径?

发布于 2024-10-30 17:20:14 字数 118 浏览 7 评论 0原文

我有一个网址。如何检索其路径部分?

例如:给定 "http://www.costo.com/test1/test2",如何获取 "test1/test2"

I have a URL. How do I retrieve its path part?

For example: Given "http://www.costo.com/test1/test2", how do I get "test1/test2"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

断念 2024-11-06 17:20:14

你想要这样的东西:

String path = new URL("http://www.costo.com/test1/test2").getPath();

实际上这会给你/test1/test2。您只需删除第一个 / 即可获得所需内容:

path = path.replaceFirst("/", "");

现在 path 中将包含 test1/test2

You want something like this:

String path = new URL("http://www.costo.com/test1/test2").getPath();

Actually that'll give you /test1/test2. You'll just have to remove the first / to get what you want:

path = path.replaceFirst("/", "");

Now you'll have test1/test2 in path.

堇年纸鸢 2024-11-06 17:20:14

我对使用 Java URL 类从 URL 中提取路径的性能产生了怀疑,并认为这有点过分了。

因此我编写了三个方法,它们都使用不同的方式从给定的 URL 中提取路径。

  1. 第一种方法使用 Java URL 类中的 URL.getPath 方法。
  2. 第二种方法使用我在 SO 中找到的正则表达式(我丢失了源链接,否则我会在这里向作者致谢)。
  3. 第三种方法使用数组拆分和连接来获得相同的结果。

对于给定的 URL,所有这三个方法都会被调用 1000000 次。

结果是:

#1 (getPathviaURL)   took:    860ms
#2 (getPathViaRegex) took:   3763ms
#3 (getPathViaSplit) took:   1365ms

代码 - 随意优化它:

public static void main(String[] args) {


        String host = "http://stackoverflow.com/questions/5564998/how-to-get-the-path-of-a-url";

        long start1 = System.currentTimeMillis();
        int i = 0;
        while (i < 1000000) {
            getPathviaURL(host);
            i++;
        }
        long end1 = System.currentTimeMillis();

        System.out.println("#1 (getPathviaURL) took: " + (end1 - start1) + "ms");
        Pattern p = Pattern.compile("(?:([^:\\/?#]+):)?(?:\\/\\/([^\\/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?");

        long start2 = System.currentTimeMillis();
        int i2 = 0;
        while (i2 < 1000000) {
            getPathViaRegex(host, p);
            i2++;
        }
        long end2 = System.currentTimeMillis();
        System.out.println("#2 (getPathViaRegex) Took: " + (end2 - start2) + "ms");

        long start3 = System.currentTimeMillis();
        int i3 = 0;
        while (i3 < 1000000) {
            getPathViaSplit(host);
            i3++;
        }
        long end3 = System.currentTimeMillis();
        System.out.println("#3 (getPathViaSplit) took: " + (end3 - start3) + "ms");



    }

    public static String getPathviaURL(String url) {
        String path = null;
        try {
            path = new URL(url).getPath();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return path;
    }

    public static String getPathViaRegex(String url, Pattern p) {
        Matcher m = p.matcher(url);

        if (m.find()) {
            return m.group(3);
        }
        return null;
    }

    public static String getPathViaSplit(String url) {
        String[] parts = url.split("/");

        parts = Arrays.copyOfRange(parts, 3, parts.length);
        String joined = "/" + StringUtils.join(parts, "/");

        return joined;
    }

I had performance doubts using the Java URL class for just extracting the path from an URL and thought that this is an overkill.

Therefore I wrote three methods, which all use a different way to extract the path from a given URL.

  1. 1st method uses the URL.getPath method from the Java URL class.
  2. 2nd method uses a regex I found in SO (I lost the source link, otherwise I'd give credits to the author right here).
  3. 3rd method uses a array-split and join for getting the same result.

All three methods are invoked 1000000 times for a given URL.

The result is:

#1 (getPathviaURL)   took:    860ms
#2 (getPathViaRegex) took:   3763ms
#3 (getPathViaSplit) took:   1365ms

Code - feel free to optimize it:

public static void main(String[] args) {


        String host = "http://stackoverflow.com/questions/5564998/how-to-get-the-path-of-a-url";

        long start1 = System.currentTimeMillis();
        int i = 0;
        while (i < 1000000) {
            getPathviaURL(host);
            i++;
        }
        long end1 = System.currentTimeMillis();

        System.out.println("#1 (getPathviaURL) took: " + (end1 - start1) + "ms");
        Pattern p = Pattern.compile("(?:([^:\\/?#]+):)?(?:\\/\\/([^\\/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?");

        long start2 = System.currentTimeMillis();
        int i2 = 0;
        while (i2 < 1000000) {
            getPathViaRegex(host, p);
            i2++;
        }
        long end2 = System.currentTimeMillis();
        System.out.println("#2 (getPathViaRegex) Took: " + (end2 - start2) + "ms");

        long start3 = System.currentTimeMillis();
        int i3 = 0;
        while (i3 < 1000000) {
            getPathViaSplit(host);
            i3++;
        }
        long end3 = System.currentTimeMillis();
        System.out.println("#3 (getPathViaSplit) took: " + (end3 - start3) + "ms");



    }

    public static String getPathviaURL(String url) {
        String path = null;
        try {
            path = new URL(url).getPath();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return path;
    }

    public static String getPathViaRegex(String url, Pattern p) {
        Matcher m = p.matcher(url);

        if (m.find()) {
            return m.group(3);
        }
        return null;
    }

    public static String getPathViaSplit(String url) {
        String[] parts = url.split("/");

        parts = Arrays.copyOfRange(parts, 3, parts.length);
        String joined = "/" + StringUtils.join(parts, "/");

        return joined;
    }
木有鱼丸 2024-11-06 17:20:14
 URL url = new  URL("http://www.google.com/in/on");
 System.out.println(url.getPath());

另请参见

 URL url = new  URL("http://www.google.com/in/on");
 System.out.println(url.getPath());

Also See

○闲身 2024-11-06 17:20:14

使用 URL 类。

use URL.getPath() method of URL class.

辞取 2024-11-06 17:20:14

你可以这样做:

    URL url = new URL("http://www.costo.com/test1/test2");
    System.out.println(url.getPath());

You can do this:

    URL url = new URL("http://www.costo.com/test1/test2");
    System.out.println(url.getPath());
浮光之海 2024-11-06 17:20:14

如果您想从应用程序的 url 获取它,例如 http://localhost:8080/ test1/test2/main.jsp
使用可以使用

request.getRequestURI() //result will be like test1/test2

If you want to get it from an url of your application something like http://localhost:8080/test1/test2/main.jsp.
Use can use

request.getRequestURI() //result will be like test1/test2
无法回应 2024-11-06 17:20:14

我建议使用 URI 类,因为它也可以处理相对路径。下面是使用 URI 和 URL 实现相同效果的示例代码:

String urlStr = "http://localhost:8080/collections-in-java?error=true";
try {
    URI uri = URI.create(urlStr);
    System.out.println(uri.getPath());
    URL url1 = new URL(urlStr);
    System.out.println(url1.getPath());
} catch (MalformedURLException e) {
    e.printStackTrace();
}

上面的代码将产生相同的结果。如果路径可能是相对的,则 URI 很有用,例如 /some/path/collections-in-java?error=true

对于这种情况,URI.getPath() code> 将返回 /some/path/collections-in-javaURL.getPath() 将抛出 MalformedURLException

I recommend to use URI class because that can handle relative path also. Here is a sample code to achieve the same with URI and URL:

String urlStr = "http://localhost:8080/collections-in-java?error=true";
try {
    URI uri = URI.create(urlStr);
    System.out.println(uri.getPath());
    URL url1 = new URL(urlStr);
    System.out.println(url1.getPath());
} catch (MalformedURLException e) {
    e.printStackTrace();
}

The above code will produce same result. The URI is useful if there is chance that the path may be relative e.g. /some/path/collections-in-java?error=true

For this case, URI.getPath() will return /some/path/collections-in-java but URL.getPath() will throw MalformedURLException.

鸩远一方 2024-11-06 17:20:14

也许晚了,但如果您不喜欢 URL 或 URI 方法,这里有一个简单的方法:

url="https://www.google.com:443//hellowrodl://here/?ff=333#2222";
url=url.split("://",2)[1];
System.out.println(url.replace(url.split("/")[0],""));

输出将是:

//hellowrodl://here/?ff=333#2222

maybe late, but if you don't like URL or URI methods, here is a simple one:

url="https://www.google.com:443//hellowrodl://here/?ff=333#2222";
url=url.split("://",2)[1];
System.out.println(url.replace(url.split("/")[0],""));

The output will be:

//hellowrodl://here/?ff=333#2222
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文