4

I'm trying to use the following approach to identify specific file types using Java. I need to implement such things in my web application.

package filetype;

import java.io.File;
import java.net.URLConnection;

final public class FileType
{
    public static void main(String[] args)
    {
        File temp=new File("G:/mountain.jpg");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));

        temp=new File("G:/myFile.txt");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));

        temp=new File("G:/zipByJava.zip");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));

        temp=new File("G:/MLM/Login.aspx");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));

        temp=new File("G:/power_point.pptx");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));

        temp=new File("G:/excel_sheet.xlsx");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));

        temp=new File("G:/word_document.docx");
        System.out.println(URLConnection.guessContentTypeFromName(temp.getAbsolutePath()));
    }
}

It displays the following output on the console.

image/jpeg
text/plain
application/zip
null
null
null
null

In the last four cases, it displays null and fails to recognize the file type of a given file. What is the best approach to identify a specific file type in Java?

Bhavesh
  • 4,607
  • 13
  • 43
  • 70
  • 2
    possible duplicate of [Getting A File's Mime Type In Java](http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java) – aioobe Mar 12 '12 at 15:37

2 Answers2

4

Use

Files.probeContentType() 

http://openjdk.java.net/projects/nio/javadoc/java/nio/file/Files.html#probeContentType(java.nio.file.Path)

And add custom detectors for any odd ball types you think you will encounter.

Edit:

Here is the oracle JavaDoc for v7

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType(java.nio.file.Path)

Java42
  • 7,628
  • 1
  • 32
  • 50
  • 1
    `java.nio.file.Files` is a part of jdk 7 and I'm working with jdk 6. I tried to search the appropriate library on the internet but to no avail. The search result says that you must have jdk 7 installed to use it. – Bhavesh Mar 12 '12 at 17:09
0

Here is the method I use to open a file with an external activity:

private void openFileWithExternalActivity(String path) {
    if (path == null) {
        return;
    }

    File file = new File(path);

    if (file.exists()) {
        Intent target = new Intent(Intent.ACTION_VIEW);

        Uri uri = FileProvider.getUriForFile(view.getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);
        String mimeType = URLConnection.guessContentTypeFromName(file.getName());

        target.setDataAndType(uri, mimeType);
        target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        try {
            view.getActivity().startActivity(target);
        } catch (ActivityNotFoundException e) {
            // TODO Instruct the user to install a viewer here
            Log.e("No viewer found", e);
        }
    }
}

The instruction

String mimeType = URLConnection.guessContentTypeFromName(file.getName());

determines the mime type

ka3ak
  • 2,435
  • 2
  • 30
  • 57