How to compare two URLs in java?

前端 未结 4 1254
逝去的感伤
逝去的感伤 2020-12-03 21:56

Here\'s a simple problem - given two urls, is there some built-in method, or an Apache library that decides whether they are (logically) equal?

For example, these tw

4条回答
  •  广开言路
    2020-12-03 22:17

    sameFile

    public boolean sameFile(URL other)Compares two URLs,

    excluding the fragment component. Returns true if this URL and the other argument are equal without taking the fragment component into consideration.

    Parameters: other - the URL to compare against. Returns: true if they reference the same remote object; false otherwise.

    also please go through this link

    http://download.oracle.com/javase/6/docs/api/java/net/URL.html#sameFile(java.net.URL)

    As iam unable to add comment , browser throwing Javascript error. so iam adding my comment here. regret for inconvience.

    //this what i suggeted

    >URL url1 = new URL("http://stackoverflow.com/foo");
    
    >URL url2 = new URL("http://stackoverflow.com/foo/");
    
    >System.out.println(url1.sameFile(url2));
    
    // this is suggested by Joachim Sauer 
    >URI uri = new URI("http://stackoverflow.com/foo/");
    >System.out.println(uri.equals("http://stackoverflow.com/foo"));
    
    // Both are giving same result
    

    so Joachim Sauer check once.

提交回复
热议问题