如何获取URL的路径?

问题描述 投票:0回答:7

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

例如:给定

"http://www.costo.com/test1/test2"
,我如何得到
"test1/test2"

java url uri
7个回答
61
投票

你想要这样的东西:

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

实际上这会给你

/test1/test2
。您只需删除第一个
/
即可获得您想要的:

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

现在您将在

test1/test2
中拥有
path


12
投票

我对使用 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;
    }

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

另见


3
投票

使用

URL.getPath()
类的
URL
方法。


3
投票

你可以这样做:

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

0
投票

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

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

0
投票

我建议使用

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()
将返回
/some/path/collections-in-java
,但
URL.getPath()
将抛出
MalformedURLException

© www.soinside.com 2019 - 2024. All rights reserved.