【问题标题】:How to get the path of a URL如何获取 URL 的路径
【发布时间】:2011-07-30 17:11:38
【问题描述】:

有一个URL,如何检索它的路径部分?

http://www.costo.com/test1/test2

如何获得“test1/test2”

【问题讨论】:

    标签: java url uri


    【解决方案1】:

    你想要这样的东西:

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

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

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

    现在您将在path 中拥有test1/test2

    【讨论】:

    • 对于任何在尝试后感到愚蠢的人,请注意它不是 javascript 代码。这是java
    【解决方案2】:

    我对使用 Java URL 类仅从 URL 中提取路径有性能疑问,并认为这是一种矫枉过正。

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

    1. 第一种方法使用 Java URL 类中的 URL.getPath 方法。
    2. 第二种方法使用了我在 SO 中找到的 regex(我丢失了源链接,否则我会在此处向作者致谢)。
    3. 第三种方法使用 array-split and join 来获得相同的结果。

    对于给定的 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;
        }
    

    【讨论】:

    • 永远不要使用System.currentTimeMillis() 进行微基准测试。使用 nano,更准确。请记住,这不是一个真正的基准测试,我强烈建议使用基准测试工具,openjdk.java.net/projects/code-tools/jmh
    • 更重要的是,永远不要过早地进行优化。我无法想象这是您应用程序中的瓶颈的情况:-)
    【解决方案3】:
     URL url = new  URL("http://www.google.com/in/on");
     System.out.println(url.getPath());
    

    另见

    【讨论】:

      【解决方案4】:

      使用URL类的URL.getPath()方法。

      【讨论】:

        【解决方案5】:

        你可以这样做:

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

            【解决方案7】:

            我建议使用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-javaURL.getPath() 将抛出 MalformedURLException

            【讨论】:

              猜你喜欢
              • 2017-01-09
              • 2019-07-23
              • 1970-01-01
              • 1970-01-01
              • 2018-11-13
              • 2019-09-06
              • 2013-11-13
              • 2011-05-28
              • 2022-08-08
              相关资源
              最近更新 更多