how to get URL using relative path

前端 未结 6 738
醉话见心
醉话见心 2020-12-15 16:33

I have a URL:

URL url=new URL(\"http://www.abc.com/aa/bb/cc/file.html\");

and a relative path:

String relativePath=\"../fil         


        
相关标签:
6条回答
  • 2020-12-15 16:51

    What I found worked is this:

    new URL("file:./src/gui/Something.fxml")
    
    0 讨论(0)
  • 2020-12-15 16:57
    new URL(url, relativePath);
    
    0 讨论(0)
  • 2020-12-15 17:00

    If you want to build an URL pointing to a relative file, you can use:

    URL url = new URL(new URL("file:"), "./myLocalFile.txt");
    
    0 讨论(0)
  • 2020-12-15 17:02

    This worked for my code
    URL url=Myclass.class.getResource("/com/fnf/si/DepAcctInq_V02.wsdl");

    0 讨论(0)
  • 2020-12-15 17:04

    Try operating winth the class URI instead of the URL.

    To get the URI from your URL:

    java.net.URI anURI=url.toUri();
    

    Then, to resolve the relative URI:

    URI resultURI=anURI.resolve(relativePath);
    

    And at last, to get an URL type use de method toUrl() of the URI result variable, and you've got it.

    0 讨论(0)
  • 2020-12-15 17:07

    When building relative URL, keep in mind that the base is path to current package, not file. I mean: the referer file is in .../myProject/src/pckg/javafile.java and the referated file is in .../myProject/some-other/nice.file, then the relative reference should look like this:

    URL url = new URL(new URL("file:"), "./../some-other/nice.file");
    

    and this works for me as well:

    URL url = new URL("file:./../some-other/nice.file");
    
    0 讨论(0)
提交回复
热议问题