Java tutorial
/* * Copyright (C) 2008 feilong * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.discovery.darchrow.io; import java.net.FileNameMap; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.FilenameUtils; import com.discovery.darchrow.util.Validator; /** * ?Mime-Type. * <p> * MIME(Multi-Purpose Internet Mail Extensions) * * @author feilong * @version 1.0.8 20141119 ?1:12:50 * @see com.baozun.nebulaplus.io.MimeType * @see "org.apache.catalina.startup.Tomcat#DEFAULT_MIME_MAPPINGS" * @see "org.apache.http.entity.ContentType" * @see <a href="http://stackoverflow.com/questions/4348810/java-library-to-find-the-mime-type-from-file-content/10140531#10140531">java * library to find the mime type from file content</a> * @see <a href="http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java">Getting A File Mime Type In Java</a> * @see <a href="http://en.wikipedia.org/wiki/MIME">MIME Wiki</a> * @see <a href="http://tika.apache.org/">?Apache Tika</a> * @since 1.0.8 */ public final class MimeTypeUtil { /** The Constant fileExtensionMap. */ private static final Map<String, String> fileExtensionMap; /** Don't let anyone instantiate this class. */ private MimeTypeUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } static { fileExtensionMap = new HashMap<String, String>(); for (MimeType mimeType : MimeType.values()) { fileExtensionMap.put(mimeType.getExtension(), mimeType.getMime()); } } /** * content type by file name.<br> * * //TODO * <b> * Very incomplete function. As of Java 7, html, pdf and jpeg extensions return the correct mime-type but js and css return null! </b> <br> * * <p> * I tried Apache Tika but it is huge with tons of dependencies, <br> * URLConnection doesn't use the bytes of the files, <br> * MimetypesFileTypeMap also just looks at files names,<br> * and I couldn't move to Java 7. * </p> * * @param fileName * the file name * @return the content type by file name * @see java.net.URLConnection#getFileNameMap() * @see javax.activation.MimetypesFileTypeMap#getContentType(java.io.File) * @see java.net.FileNameMap#getContentTypeFor(String) * @see org.apache.commons.io.FilenameUtils#getExtension(String) * @see java.net.URLConnection#guessContentTypeFromName(String) * @see java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream) * @see "java.nio.file.Files#probeContentType(java.nio.file.Path)" */ public static String getContentTypeByFileName(String fileName) { String extension = FilenameUtils.getExtension(fileName); if (Validator.isNullOrEmpty(extension)) { return null; } // 1. first use java's build-in utils //??? mimetable? "content.types.user.table" ?? java lib/content-types.properties FileNameMap fileNameMap = URLConnection.getFileNameMap(); String contentType = fileNameMap.getContentTypeFor(fileName); // 2. nothing found -> lookup our in extension map to find types like ".doc" or ".docx" if (Validator.isNullOrEmpty(contentType)) { contentType = fileExtensionMap.get(extension.toLowerCase()); } return contentType; } }