Java獲取檔案ContentType案例
阿新 • • 發佈:2020-08-25
原始碼如下:
package com.oysept; import java.io.File; import java.io.IOException; import java.net.FileNameMap; import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.activation.MimetypesFileTypeMap; /** * Java獲取檔案ContentType * @author ouyangjun */ public class ContentTypeUtils { public static void main(String[] args) { // 檔案路徑 String fileUrl = "C:\\Users\\admin\\Desktop\\tttt.rar"; // 方式一 getContentTypeByLocal(fileUrl); // 方式二,推薦使用 getContentType(fileUrl); // 方式三 getContentTypeByType(fileUrl); } /** * 方式一 * 該方式只支援本地檔案,有時候會存在獲取為null的情況 * @param fileUrl */ public static String getContentTypeByLocal(String fileUrl) { String contentType = null; Path path = Paths.get(fileUrl); try { contentType = Files.probeContentType(path); } catch (IOException e) { e.printStackTrace(); } System.out.println("getContentTypeByLocal,File ContentType is : " + contentType); return contentType; } /** * 方式二 * 該方式支援本地檔案,也支援http/https遠端檔案 * @param fileUrl */ public static String getContentType(String fileUrl) { String contentType = null; try { contentType = new MimetypesFileTypeMap().getContentType(new File(fileUrl)); } catch (Exception e) { e.printStackTrace(); } System.out.println("getContentType,File ContentType is : " + contentType); return contentType; } /** * 方式三 * @param fileUrl,有時候會存在獲取為null的情況 */ public static String getContentTypeByType(String fileUrl) { String contentType = null; try { FileNameMap fileNameMap = URLConnection.getFileNameMap(); contentType = fileNameMap.getContentTypeFor(fileUrl); } catch (Exception e) { e.printStackTrace(); } System.out.println("getContentTypeByType,File ContentType is : " + contentType); return contentType; } }
列印效果圖:
補充知識:ImageTypeUtil工具類:Java獲取URL對應的檔案型別及其後綴
Java獲取URL對應的檔案型別及其後綴的主流方法有三種:
1、根據檔案頭部資料來判斷。
通常需要先下載再判斷,但是如果想要在下載的時候確定檔案字尾,就做不到了,而且獲取的檔案型別不是很準確。
2、使用lastIndexOf去解析url字串。
這種方法最簡單高效。
3、UrlConnection獲取ContentType的型別推測出檔案的型別。
這裡我封裝了一個工具類,將第二種方法和第三種方法結合,但是不是用lastIndexOf,而是判斷url字串是否包含圖片的字尾。
package johny.utils; import java.net.URLConnection; /** * @author Johny 林子豪 */ public enum ImageTypeUtil { PNG(".png","image/png"),JPG(".jpg","image/jpeg"),BMP(".bmp","image/bmp"),JPEG(".jpeg",GIF(".gif","image/gif"),TIF(".tif","image/tiff"),//標籤影象檔案格式(Tagged Image File Format,簡寫為TIFF)是一種主要用來儲存包括照片和藝術圖在內的影象的檔案格式。它最初由Aldus公司與微軟公司一起為PostScript列印開發。 TIFF(".tiff",FAX(".fax","image/fax"),ICO(".ico","image/x-icon"),JFIF(".jfif",JPE(".jpe",NET(".net","image/pnetvue"),WBMP(".wbmp","image/vnd.wap.wbmp"); //如果有其他的mime型別, /** * 字尾名 */ final String mSuffix; final String mMIME; ImageTypeUtil(String suffix,String mime) { this.mSuffix = suffix; this.mMIME = mime; } public static String getSuffixFromUrl(String url) { for (ImageTypeUtil fileType : values()) { if (url.contains(fileType.suffix())) { return fileType.suffix(); } } String contentType = getMIMETypeFromUrl(url); if (contentType == null) return null; return mimeMapingSuffix(contentType); } public static String getMIMETypeFromUrl(String url) { if (url == null || url.isEmpty()) { return null; } return URLConnection.guessContentTypeFromName(url); } /** * mime型別對應的字尾名 */ public static String mimeMapingSuffix(String mime) { for (ImageTypeUtil fileType : values()) { if (fileType.mime().equals(mime)) { return fileType.suffix(); } } return null; } public String mime() { return mMIME; } /** * 獲取字尾名 * * @return 指定型別的字尾名,如'.mp4' */ public String suffix() { return this.mSuffix; } }
以上這篇Java獲取檔案ContentType案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。