Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4975103

URLConnection.getContentType() does not always return content-type header field

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 5.0
    • 5.0
    • core-libs
    • b36
    • sparc
    • solaris_8

      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"));
          }
      }

            jccollet Jean-Christophe Collet (Inactive)
            xwangsunw Xiaozhong Wang (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: