Java Activation Mimetype Check isImage(String fileName, boolean output)

Here you can find the source of isImage(String fileName, boolean output)

Description

Obtains a files MIME type and returns true if it is of type image

License

Open Source License

Parameter

Parameter Description
fileName a parameter
output a parameter

Return

true if an image; false otherwise

Declaration

public static boolean isImage(String fileName, boolean output) 

Method Source Code

//package com.java2s;

import javax.activation.MimetypesFileTypeMap;

public class Main {
    /**//from  w w  w .j a va  2  s . com
     * Obtains a files MIME type and returns true if it is of type image
     * @param fileName
     * @param output
     * @return true if an image; false otherwise
     */
    public static boolean isImage(String fileName, boolean output) {

        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String mimeType = mimeTypesMap.getContentType(fileName);

        if (output) {
            System.out.println("Validating input file...");
            System.out.println("File type: " + mimeType);
        }

        if (mimeType.startsWith("image")) {
            return true;
        } else {

            if (output) {
                System.out.println("Invalid file type input. Please enter an image");
            }

            return false;
        }

    }
}

Related

  1. getMimetype(String filename)
  2. getMimeTypeForFileName(String filename)
  3. init()
  4. initFileTypeMap()
  5. isFileImage(String fileName)
  6. isMimeType(String a)
  7. isValidateContentType(String contentType)
  8. setMinetypes()