1313 * @date 2024年05月08 17:29
1414 **/
1515public class FileUtil {
16- private static final Map <String , String > mimeMap = new HashMap <>();
16+ private static final Map <String , String > MIME_TO_EXTENSION = new HashMap <>();
17+ private static final Map <String , String > EXTENSION_TO_MIME = new HashMap <>();
1718
1819 static {
19- mimeMap .put (".c" , "text/x-c" );
20- mimeMap .put (".cs" , "text/x-csharp" );
21- mimeMap .put (".cpp" , "text/x-c++" );
22- mimeMap .put (".doc" , "application/msword" );
23- mimeMap .put (".docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" );
24- mimeMap .put (".html" , "text/html" );
25- mimeMap .put (".java" , "text/x-java" );
26- mimeMap .put (".json" , "application/json" );
27- mimeMap .put (".md" , "text/markdown" );
28- mimeMap .put (".pdf" , "application/pdf" );
29- mimeMap .put (".php" , "text/x-php" );
30- mimeMap .put (".pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation" );
31- mimeMap .put (".py" , "text/x-python" );
32- mimeMap .put (".rb" , "text/x-ruby" );
33- mimeMap .put (".tex" , "text/x-tex" );
34- mimeMap .put (".txt" , "text/plain" );
35- mimeMap .put (".css" , "text/css" );
36- mimeMap .put (".js" , "text/javascript" );
37- mimeMap .put (".sh" , "application/x-sh" );
38- mimeMap .put (".ts" , "application/typescript" );
20+ // 初始化MIME类型到文件扩展名的映射
21+ MIME_TO_EXTENSION .put ("text/html" , "html" );
22+ MIME_TO_EXTENSION .put ("text/css" , "css" );
23+ MIME_TO_EXTENSION .put ("text/javascript" , "js" );
24+ MIME_TO_EXTENSION .put ("image/jpg" , "jpg" );
25+ MIME_TO_EXTENSION .put ("image/jpeg" , "jpg" );
26+ MIME_TO_EXTENSION .put ("image/png" , "png" );
27+ MIME_TO_EXTENSION .put ("image/gif" , "gif" );
28+ MIME_TO_EXTENSION .put ("image/webp" , "webp" );
29+ MIME_TO_EXTENSION .put ("image/svg+xml" , "svg" );
30+ MIME_TO_EXTENSION .put ("application/pdf" , "pdf" );
31+ MIME_TO_EXTENSION .put ("application/zip" , "zip" );
32+ MIME_TO_EXTENSION .put ("audio/mpeg" , "mp3" );
33+ MIME_TO_EXTENSION .put ("audio/mp3" , "mp3" );
34+ MIME_TO_EXTENSION .put ("audio/mp4" , "m4a" );
35+ MIME_TO_EXTENSION .put ("audio/wav" , "wav" );
36+ MIME_TO_EXTENSION .put ("audio/wave" , "wav" );
37+ MIME_TO_EXTENSION .put ("audio/webm" , "webm" );
38+ MIME_TO_EXTENSION .put ("audio/amr" , "amr" );
39+ MIME_TO_EXTENSION .put ("video/mp4" , "mp4" );
40+ MIME_TO_EXTENSION .put ("video/quicktime" , "mov" );
41+ MIME_TO_EXTENSION .put ("video/mpeg" , "mpeg" );
42+
43+ // Microsoft Word文档
44+ MIME_TO_EXTENSION .put ("application/msword" , "doc" );
45+ MIME_TO_EXTENSION .put ("application/json" , "json" );
46+ MIME_TO_EXTENSION .put ("application/vnd.openxmlformats-officedocument.wordprocessingml.document" , "docx" );
47+
48+ // Microsoft Excel电子表格
49+ MIME_TO_EXTENSION .put ("application/vnd.ms-excel" , "xls" );
50+ MIME_TO_EXTENSION .put ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" , "xlsx" );
51+
52+ MIME_TO_EXTENSION .put ("application/vnd.ms-powerpoint" , "ppt" );
53+ MIME_TO_EXTENSION .put ("application/vnd.openxmlformats-officedocument.presentationml.presentation" , "pptx" );
54+
55+ MIME_TO_EXTENSION .put ("text/plain" , "txt" );
56+ MIME_TO_EXTENSION .put ("text/markdown" , "md" );
57+ MIME_TO_EXTENSION .put ("text/csv" , "csv" );
58+
59+ MIME_TO_EXTENSION .put ("application/xml" , "xml" );
60+ MIME_TO_EXTENSION .put ("application/epub+zip" , "epub" );
61+ MIME_TO_EXTENSION .put ("message/rfc822" , "eml" );
62+ MIME_TO_EXTENSION .put ("application/vnd.ms-outlook" , "msg" );
63+
64+ // 编程语言相关的MIME类型
65+ MIME_TO_EXTENSION .put ("text/x-c" , "c" );
66+ MIME_TO_EXTENSION .put ("text/x-csharp" , "cs" );
67+ MIME_TO_EXTENSION .put ("text/x-c++" , "cpp" );
68+ MIME_TO_EXTENSION .put ("text/x-java" , "java" );
69+ MIME_TO_EXTENSION .put ("text/x-php" , "php" );
70+ MIME_TO_EXTENSION .put ("text/x-python" , "py" );
71+ MIME_TO_EXTENSION .put ("text/x-ruby" , "rb" );
72+ MIME_TO_EXTENSION .put ("text/x-tex" , "tex" );
73+ MIME_TO_EXTENSION .put ("application/x-sh" , "sh" );
74+ MIME_TO_EXTENSION .put ("application/typescript" , "ts" );
75+ // 可以根据需要添加更多的MIME类型
76+
77+ EXTENSION_TO_MIME .put ("html" , "text/html" );
78+ EXTENSION_TO_MIME .put ("css" , "text/css" );
79+ EXTENSION_TO_MIME .put ("js" , "text/javascript" );
80+ EXTENSION_TO_MIME .put ("jpg" , "image/jpg" );
81+ EXTENSION_TO_MIME .put ("jpeg" , "image/jpeg" );
82+ EXTENSION_TO_MIME .put ("png" , "image/png" );
83+ EXTENSION_TO_MIME .put ("gif" , "image/gif" );
84+ EXTENSION_TO_MIME .put ("webp" , "image/webp" );
85+ EXTENSION_TO_MIME .put ("svg" , "image/svg+xml" );
86+ EXTENSION_TO_MIME .put ("pdf" , "application/pdf" );
87+ EXTENSION_TO_MIME .put ("zip" , "application/zip" );
88+ EXTENSION_TO_MIME .put ("mp3" , "audio/mpeg" );
89+ EXTENSION_TO_MIME .put ("m4a" , "audio/mp4" );
90+ EXTENSION_TO_MIME .put ("wav" , "audio/wav" );
91+ EXTENSION_TO_MIME .put ("webm" , "audio/webm" );
92+ EXTENSION_TO_MIME .put ("amr" , "audio/amr" );
93+ EXTENSION_TO_MIME .put ("mp4" , "video/mp4" );
94+ EXTENSION_TO_MIME .put ("mov" , "video/quicktime" );
95+ EXTENSION_TO_MIME .put ("mpeg" , "video/mpeg" );
96+ EXTENSION_TO_MIME .put ("c" , "text/x-c" );
97+ EXTENSION_TO_MIME .put ("cs" , "text/x-csharp" );
98+ EXTENSION_TO_MIME .put ("cpp" , "text/x-c++" );
99+ EXTENSION_TO_MIME .put ("java" , "text/x-java" );
100+ EXTENSION_TO_MIME .put ("php" , "text/x-php" );
101+ EXTENSION_TO_MIME .put ("py" , "text/x-python" );
102+ EXTENSION_TO_MIME .put ("rb" , "text/x-ruby" );
103+ EXTENSION_TO_MIME .put ("tex" , "text/x-tex" );
104+ EXTENSION_TO_MIME .put ("sh" , "application/x-sh" );
105+ EXTENSION_TO_MIME .put ("ts" , "application/typescript" );
106+ EXTENSION_TO_MIME .put ("doc" , "application/msword" );
107+ EXTENSION_TO_MIME .put ("json" , "application/json" );
108+ EXTENSION_TO_MIME .put ("docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" );
109+ EXTENSION_TO_MIME .put ("xls" , "application/vnd.ms-excel" );
110+ EXTENSION_TO_MIME .put ("xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
111+ EXTENSION_TO_MIME .put ("ppt" , "application/vnd.ms-powerpoint" );
112+ EXTENSION_TO_MIME .put ("pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation" );
113+ EXTENSION_TO_MIME .put ("txt" , "text/plain" );
114+ EXTENSION_TO_MIME .put ("md" , "text/markdown" );
115+ EXTENSION_TO_MIME .put ("csv" , "text/csv" );
116+ EXTENSION_TO_MIME .put ("xml" , "application/xml" );
117+ EXTENSION_TO_MIME .put ("epub" , "application/epub+zip" );
118+ EXTENSION_TO_MIME .put ("eml" , "message/rfc822" );
119+ EXTENSION_TO_MIME .put ("msg" , "application/vnd.ms-outlook" );
120+
121+
39122 }
40123
41124 public static MediaType getFileUploadMediaType (String fileName ) {
42- return MediaType .parse (mimeMap .getOrDefault (getFileExtension (fileName ), "text/plain" ));
125+ return MediaType .parse (EXTENSION_TO_MIME .getOrDefault (getFileExtension (fileName ), "text/plain" ));
126+ }
127+
128+ public static String getSubType (MediaType mediaType ) {
129+ if (mediaType == null ) {
130+ return null ;
131+ }
132+ return mediaType .subtype ();
133+ }
134+
135+ public static String extraPureMediaType (MediaType mediaType ) {
136+ return mediaType .type () + "/" + mediaType .subtype ();
43137 }
44138
45139 public static String getFileExtension (String filename ) {
46140 int dotIndex = filename .lastIndexOf ('.' );
47141 if (dotIndex >= 0 && dotIndex < filename .length () - 1 ) {
48- return filename .substring (dotIndex ); // Includes the dot
142+ return filename .substring (dotIndex + 1 ); // Includes the dot
49143 }
50144 return "" ; // No extension found
51145 }
@@ -59,7 +153,7 @@ public static String getFileExtension(String filename) {
59153 public static byte [] readAllBytes (InputStream inputStream ) {
60154 try (ByteArrayOutputStream buffer = new ByteArrayOutputStream ()) {
61155 int nRead ;
62- byte [] data = new byte [2048 ];
156+ byte [] data = new byte [8192 ];
63157 while ((nRead = inputStream .read (data , 0 , data .length )) != -1 ) {
64158 buffer .write (data , 0 , nRead );
65159 }
0 commit comments