Java Tutorial

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 List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URLConnection guessContentTypeFromStream()



Description

The Java URLConnection guessContentTypeFromStream() method tries to determine the type of an input stream based on the characters at the beginning of the input stream. This method can be used by subclasses that override the getContentType method.

Declaration

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

public static String guessContentTypeFromStream(InputStream is) throws IOException

Parameters

is − an input stream that supports marks.

Return Value

a guess at the content type, or null if none can be determined.

Exception

IOException − if an I/O error occurs while getting the content.

Example 1

The following example shows the usage of Java URLConnection guessContentTypeFromStream() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getInputStream(), we're getting the inputStream instance and then using guessContentTypeFromStream() method, we're getting content type of home page and printing the same −

package com.tutorialspoint;

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

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();

         String contentType = URLConnection.guessContentTypeFromStream(urlConnection.getInputStream());
         System.out.println(contentType);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

null

Example 1

The following example shows the usage of Java URLConnection guessContentTypeFromStream() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getInputStream(), we're getting the inputStream instance and then using guessContentTypeFromStream() method, we're getting content type of home page and printing the same −

package com.tutorialspoint;

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

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();

         String contentType = URLConnection.guessContentTypeFromStream(urlConnection.getInputStream());
         System.out.println(contentType);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

null

Example 3

The following example shows the usage of Java URLConnection guessContentTypeFromStream() method for a valid url of google with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getInputStream(), we're getting the inputStream instance and then using guessContentTypeFromStream() method, we're getting content type of home page and printing the same −

package com.tutorialspoint;

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

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         String contentType = URLConnection.guessContentTypeFromStream(urlConnection.getInputStream());
         System.out.println(contentType);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

null
java_urlconnection.htm
Advertisements