Selected Reading

Java - URLConnection getFileNameMap() Method



Description

The Java URLConnection getFileNameMap() method loads filename map (a mimetable) from a data file. It will first try to load the user-specific table, defined by "content.types.user.table" property. If that fails, it tries to load the default built-in table.

Declaration

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

public static FileNameMap getFileNameMap()

Parameters

NA

Return Value

the FileNameMap.

Exception

NA

Example 1

The following example shows the usage of Java URLConnection getFileNameMap() method. In this example, we're getting an instance of FileNameMap interface using the URLConnection.getFileNameMap() method. Now using FileNameMap instance, we're printing content type of index.htm file name −

package com.tutorialspoint;

import java.net.FileNameMap;
import java.net.URLConnection;

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

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 getFileNameMap() method. In this example, we're getting an instance of FileNameMap interface using the URLConnection.getFileNameMap() method. Now using FileNameMap instance, we're printing content type of logo.png file name −

package com.tutorialspoint;

import java.net.FileNameMap;
import java.net.URLConnection;

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

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 getFileNameMap() method. In this example, we're getting an instance of FileNameMap interface using the URLConnection.getFileNameMap() method. Now using FileNameMap instance, we're printing content type of architecture.jpg file name −

package com.tutorialspoint;

import java.net.FileNameMap;
import java.net.URLConnection;

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

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

Output

image/jpeg
java_urlconnection.htm
Advertisements