The specification of URLConnection.getContentType() says:
Returns the value of the content-type header field.
Returns:
the content type of the resource that the URL references, or null if not known.
However, if the URL is a file URL pointing to a plain file,
URLConnection.getContentType() returns "content/unknown" but content-type
header field is null.
import java.net.*;
public class ContentTypeTest {
public static void main(String[] args) throws Exception {
URL url = new URL("file:///etc/passwd");
URLConnection conn = url.openConnection();
conn.connect();
System.out.println(conn.getContentType());
System.out.println(conn.getHeaderField("content-type"));
}
}
Returns the value of the content-type header field.
Returns:
the content type of the resource that the URL references, or null if not known.
However, if the URL is a file URL pointing to a plain file,
URLConnection.getContentType() returns "content/unknown" but content-type
header field is null.
import java.net.*;
public class ContentTypeTest {
public static void main(String[] args) throws Exception {
URL url = new URL("file:///etc/passwd");
URLConnection conn = url.openConnection();
conn.connect();
System.out.println(conn.getContentType());
System.out.println(conn.getHeaderField("content-type"));
}
}