Here you can find the source of formatNameToMimeType(final String formatName)
Parameter | Description |
---|---|
format | name The format name of an image file. |
Parameter | Description |
---|---|
IIOException | if no image reader are able to handle the format name given. |
public static String formatNameToMimeType(final String formatName) throws IIOException
//package com.java2s; /*/*from w w w . j a v a 2 s . c o m*/ * Geotoolkit - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2004 - 2008, Open Source Geospatial Foundation (OSGeo) * (C) 2008 - 2010, Geomatys * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.util.Iterator; import javax.imageio.IIOException; import javax.imageio.spi.IIORegistry; import javax.imageio.spi.ImageReaderSpi; public class Main { /** * Returns the mime type matching the format name of an image file. * For example, for a format name "png" it will return "image/png", in most cases. * * @param format name The format name of an image file. * @return The mime type for the format name specified. * * @throws IIOException if no image reader are able to handle the format name given. */ public static String formatNameToMimeType(final String formatName) throws IIOException { final Iterator<ImageReaderSpi> readers = IIORegistry.lookupProviders(ImageReaderSpi.class); while (readers.hasNext()) { final ImageReaderSpi reader = readers.next(); final String[] formats = reader.getFormatNames(); for (String format : formats) { if (formatName.equalsIgnoreCase(format)) { final String[] mimeTypes = reader.getMIMETypes(); if (mimeTypes != null && mimeTypes.length > 0) { return mimeTypes[0]; } } } } throw new IIOException("No available image reader able to handle the format name specified: " + formatName); } }