Java: how to get a File from an escaped URL?

前端 未结 3 503
挽巷
挽巷 2020-12-03 09:30

I\'m receiving an URL that locates a local file (the fact that I receive an URL is not in my control). The URL is escaped validly as defined in RFC2396. How can I transfor

相关标签:
3条回答
  • 2020-12-03 10:10

    The File constructor taking an URI in combination with URL#toURI() should work:

    URL url = getItSomehow();
    File file = new File(url.toURI());
    
    0 讨论(0)
  • 2020-12-03 10:18

    AFAIK like this Paths.get(url.toURI()).toFile() is best, starting with Java 7. See also Converting Java file:// URL to File(...) path, platform independent, including UNC paths.

    0 讨论(0)
  • 2020-12-03 10:24
    URLDecoder.decode(url);//deprecated
    URLDecoder.decode(url, "UTF-8"); //use this instead
    

    See related question How do you unescape URLs in Java?

    0 讨论(0)
提交回复
热议问题