Tag Archives: head

Get ContentType for URL in java

content-type
To determine content type for certain URL you can use next:

package org.test;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class ContentType {

    public static void main(String[] args) throws IOException {
        getContentType("http://google.com");
        getContentType("http://www.orimi.com/pdf-test.pdf");
    }

    private static void getContentType(String uri) throws IOException {
        URL url = new URL(uri);
        HttpURLConnection connection = (HttpURLConnection)  url.openConnection();
        connection.setRequestMethod("HEAD");
        connection.connect();

        System.out.println(connection.getContentType() + ": " 
          + connection.getResponseCode() + " -> " + uri);
    }
}

The result:

text/html; charset=windows-1251: 200 -> http://google.com
application/pdf: 200 -> http://www.orimi.com/pdf-test.pdf

So, as you see to get content type it is need to send small HEAD request!
Thanx to http://stackoverflow.com/a/5802223