源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
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字符串是否包含圖片的后綴。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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" , "image/jpeg" ), GIF( ".gif" , "image/gif" ), TIF( ".tif" , "image/tiff" ), //標簽圖像文件格式(Tagged Image File Format,簡寫為TIFF)是一種主要用來存儲包括照片和藝術圖在內的圖像的文件格式。它最初由Aldus公司與微軟公司一起為PostScript打印開發。 TIFF( ".tiff" , "image/tiff" ), FAX( ".fax" , "image/fax" ), ICO( ".ico" , "image/x-icon" ), JFIF( ".jfif" , "image/jpeg" ), JPE( ".jpe" , "image/jpeg" ), 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案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/p812438109/article/details/83587315