tags:

views:

46

answers:

1

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.org/2001/08/baseball/baseball-ont is http://www.daml.org/2001/08/baseball/baseball-ont.owl

When I do

    URI uri = new URI(address); 
    URL url = uri.toURL();
    String file = url.getFile();
    System.out.println(file);

I am not able to see the full file name with .owl extension, just /2001/08/baseball/baseball-ont how do I get the file extension as well. ``

+1  A: 

There are two answers to this.

If a URI does not have a "file extension", then there is no way that you can infer one by looking at it textually, or by converting it to a File. In general, neither the URI or the File needs to have an extension at all. Extensions are just a file naming convention.

What you are really after is the media type / MIMEtype / content type of the file. You may be able to determine the media type by doing something like this:

URLConnection conn = url.connect();
String type = conn.getContentType();

However the getContentType() method may return null if it is not possible to determine or retrieve a content type.

Stephen C