URLConnection.guessContentTypeFromStream(InputStream) in Java

URLConnection.guessContentTypeFromStream(InputStream): This method is available in java.net.URLConnection class of Java.

Syntax:

String java.net.URLConnection.guessContentTypeFromStream(InputStream is) throws IOException

This method takes one argument. This method tries to determine the type of 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.

Ideally, this routine would not be needed. But many http servers return the incorrect content type; in addition, there are many nonstandard extensions. Direct inspection of the bytes to determine the content type is often more accurate than believing the content type claimed by the http server.

Parameters: One parameter is required for this method.

is: an input stream that supports marks.

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

Throws:

1. IOException - if an I/O error occurs while reading the input stream.

Approach

Java

package com.URLConnection;

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

public class URLConnectionguessContentTypeFromStream {
    public static void main(String[] args) throws IOException {

        InputStream is = InputStream.nullInputStream();
        System.out.println(URLConnection.guessContentTypeFromStream(is));
    }
}

Output:

null


Some other methods of URLConnection class

addRequestProperty(String, String)This method adds a general request property specified by a key-value pair.

connect()This method opens a communications link to the resource referenced by this URL if such a connection has not already been established.

getAllowUserInteraction()This method returns the value of the allowUserInteraction field for this object.

getConnectTimeout()This method returns the setting for connect timeout.

getContent()This method retrieves the contents of this URL connection.

getContent(Class<?>[]) This method retrieves the contents of this URL connection.

getContentEncoding()This method returns the value of the content-encoding header field.

getContentLength()This method returns the value of the content-length header field.

getContentLengthLong()This method returns the value of the content-length header field as long.

getContentType()This method returns the value of the content-type header field.

getDate()This method returns the value of the Date header field.

URLConnection.getDefaultAllowUserInteraction()This method returns the default value of the allowUserInteractionfield.

getDefaultUseCaches()This method returns the default value of a URLConnection's useCaches flag.

URLConnection.getDefaultUseCaches(String)This method returns the default value of the useCaches flag for the given protocol.

getDoInput()This method returns the value of this URLConnection's input flag.

getDoOutput()This method returns the value of this URLConnection's output flag.

getExpiration()This method returns the value of the expires header field.

URLConnection.getFileNameMap()This method loads a filename map (a mimetable) from a data file.

getHeaderField(int)This method returns the value for the nth header field. It returns null if there are fewer than n+1 fields.

getHeaderField(String)This method returns the value of the named header field.

getHeaderFieldDate(String, long)This method returns the value of the named field parsed as a date.

getHeaderFieldInt(String, int)This method returns the value of the named field parsed as a number.

getHeaderFieldKey(int)This method returns the key for the nth header field. 

getHeaderFieldLong(String, long)This method returns the value of the named field parsed as a number.

getHeaderFields()This method returns an unmodifiable Map of the header fields.

getIfModifiedSince()This method returns the value of this object's ifModifiedSince field.

getInputStream()This method returns an input stream that reads from this open connection.

getLastModified()This method returns the value of the last-modified header field. 

getOutputStream()This method returns an output stream that writes to this connection.

getPermission()This method returns a permission object representing the permission necessary to make the connection represented by this object.

getReadTimeout()This method returns a setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).

getRequestProperties()This method returns an unmodifiable Map of general request properties for this connection.

getRequestProperty(String)This method returns the value of the named general request property for this connection.

getURL()This method returns the value of this URLConnection's URL field.

getUseCaches()This method returns the value of this URLConnection's useCaches field.

URLConnection.guessContentTypeFromName(String)This method tries to determine the content type of an object, based on the specified  "file" component of a URL.

URLConnection.guessContentTypeFromStream(InputStream)This method tries to determine the type of input stream based on the characters at the beginning of the input stream.

setAllowUserInteraction(boolean)This method set the value of the allowUserInteraction field of this URLConnection.

setConnectTimeout(int)This method sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.

URLConnection.setContentHandlerFactory(ContentHandlerFactory)This method sets the ContentHandlerFactory of an application. It can be called at most once by an application.

setDefaultUseCaches(boolean)This method sets the default value of the useCaches field to the specified value.

setDefaultUseCaches(String, boolean)This method sets the default value of the useCaches field for the named protocol to the given value.

setDoInput(boolean)This method sets the value of the doInput field for this URLConnection to the specified value.

setDoOutput(boolean)This method sets the value of the doOutput field for this URLConnection to the specified value.

URLConnection.setFileNameMap(FileNameMap):  This method sets the FileNameMap.

setIfModifiedSince(long)This method sets the value of the ifModifiedSince field of this URLConnection to the specified value.

setReadTimeout(int)This method sets the read timeout to a specified timeout, in milliseconds.

setRequestProperty(String, String)This method sets the general request property. If a property with the key already exists, overwrite its value with the new value.

setUseCaches(boolean)This method sets the value of the useCaches field of this URLConnection to the specified value.

toString()This method returns a String representation of this URL connection.

No comments:

Post a Comment