How to determine the file extension of a file from a uri

后端 未结 8 1853
一向
一向 2020-12-13 19:06

Assuming I am given a URI, and I want to find the file extension of the file that is returned, what do I have to do in Java.

For example the file at http://www.daml.

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 19:49

    URLConnection.guessContentTypeFromName(url) would deliver the mime type as in the first answer. Maybe you simply wanted:

    String extension = url.getPath().replaceFirst("^.*/[^/]*(\\.[^\\./]*|)$", "$1");
    

    The regular expression consuming all upto the last slash, then upto a period and either returns an extension like ".owl" or "". (If not mistaken)

提交回复
热议问题