Java Operators

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - URLConnection guessContentTypeFromName()



Description

The Java URLConnection guessContentTypeFromName() method tries to determine the content type of an object, based on the specified "file" component of a URL. This is a convenience method that can be used by subclasses that override the getContentType method.

Declaration

Following is the declaration for java.net.URLConnection.guessContentTypeFromName() method

public static String guessContentTypeFromName(String fname)

Parameters

fname − a filename.

Return Value

a guess as to what the content type of the object is, based upon its file name.

Exception

NA

Example 1

The following example shows the usage of Java URLConnection guessContentTypeFromName() method. In this example, we're getting content type of a file using the URLConnection.guessContentTypeFromName() method and printing content type of index.htm file name −

package com.tutorialspoint;

import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      String contentType = URLConnection.guessContentTypeFromName("index.htm");
      System.out.println(contentType);
   }
}

Let us compile and run the above program, this will produce the following result −

Output

text/html

Example 2

The following example shows the usage of Java URLConnection guessContentTypeFromName() method. In this example, we're getting content type of a file using the URLConnection.guessContentTypeFromName() method and printing content type of logo.png file name −

package com.tutorialspoint;

import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      String contentType = URLConnection.guessContentTypeFromName("logo.png");
      System.out.println(contentType);
   }
}

Let us compile and run the above program, this will produce the following result −

Output

image/png

Example 3

The following example shows the usage of Java URLConnection guessContentTypeFromName() method.In this example, we're getting content type of a file using the URLConnection.guessContentTypeFromName() method and printing content type of architecture.jpg file name −

package com.tutorialspoint;

import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      String contentType = URLConnection.guessContentTypeFromName("architecture.jpg");
      System.out.println(contentType);
   }
}

Let us compile and run the above program, this will produce the following result −

Output

image/jpeg
java_urlconnection.htm