可以使用內建函式庫中的 javax.activation.MimetypesFileTypeMap 來處理
此方法只能快速的判斷檔案類型,當然不止image,還有其他更多檔案類型,有興趣可以參考,這主要是拿來判斷網路上檔案用的。
application/javascript js
application/msword doc docx docm
application/pdf pdf
application/postscript ai eps ps
application/rss+xml rss
application/rtf rtf
application/vnd.ms-excel xls xlsx xlsm XLS
application/vnd.ms-powerpoint ppt pps pot pptx pptm
application/vnd.oasis.database odb
application/vnd.oasis.opendocument.text odt
application/vnd.oasis.presentation odp
application/vnd.oasis.spreadsheet ods
application/vnd.oasis.text odt
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/x-awk awk
application/x-blender blend
application/x-cd-image iso
application/x-compress zip gz tar rar
application/x-deb deb
application/x-font-otf otf OTF
application/x-font-ttf ttf TTF
application/x-java-applet class
application/x-java-archive jar
application/xml xml
application/x-ms-dos-executable exe msi
application/x-perl pl
application/x-php php
application/x-rpm rpm
application/x-sharedlib o
application/x-shellscript sh
application/x-tar tar
application/x-texinfo texinfo texi
application/x-tex tex
application/x-trash autosave
application/x-troff t tr roff
application/x-vnd.oasis.opendocument.spreadsheet ods
application/zip zip
audio/ac3 ac3
audio/basic au
audio/midi midi mid
audio/mpeg mp3 mpeg3
audio/x-aifc aifc
audio/x-aiff aif aiff
audio/x-generic wav wma mp3 ogg
audio/x-mpeg mpeg mpg
audio/x-wav wav
image/gif gif GIF
image/ief ief
image/jpeg jpeg jpg jpe JPG
image/png png PNG
image/svg+xml svg svgz
image/tiff tiff tif
image/x-eps eps
image/x-generic bmp jpg jpeg png tif tiff xpm wmf emf
image/x-xwindowdump xwd
text/css css
text/csv csv
text/html html htm HTML HTM
text/plain txt text TXT TEXT
text/richtext rtx
text/rtf rtf
text/tab-separated-values tsv tab
text/x-bibtex bib
text/x-c++hdr h
text/x-csrc c
text/x-c++src cpp c++
text/x-java java
text/x-log log
text/xml xml XML osm
text/x-pascal pas
text/x-po po pot
text/x-python py
text/x-sql sql
text/x-tcl tcl
text/x-tex tex
video/mpeg mpeg mpg mpe mpv vbs mpegv
video/msvideo avi
video/quicktime qt mov moov
video/x-generic wmv mpeg mp4 ogv swf mov dvd osp
video/x-msvideo avi
我們只需要判斷是否為image 開頭即可
package DefaultPackage;
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
class ImageTester {
public static void main(String[] args) {
ImageTester it = new ImageTester();
String filepath = "/Users/quanto/Git/Qu_Photo/PhotoInfo_1.0.0.jar";
if (ImageTester.isImage(filepath))
System.out.println("It's an image");
else
System.out.println("It's NOT an image");
filepath = "/Users/quanto/Git/Qu_Photo/2024-04-28_135938.JPG";
if (ImageTester.isImage(filepath))
System.out.println("It's an image");
else
System.out.println("It's NOT an image");
}
public static Boolean isImage(String filepath) {
File f = new File(filepath);
String mimetype = new MimetypesFileTypeMap().getContentType(f);
String type = mimetype.split("/")[0];
if (type.equals("image"))
return true;
else
return false;
}
public static Boolean isImage(File f) {
String mimetype = new MimetypesFileTypeMap().getContentType(f);
String type = mimetype.split("/")[0];
if (type.equals("image"))
return true;
else
return false;
}
}
此範例中,會先show 出 It's an image
再接著 show 出 It's NOT an image
利用這個工具可以使用原生java 快速判斷檔案類型
參考資料:https://github.com/jjYBdx4IL/misc/blob/master/text-utils/src/main/resources/com/github/jjYBdx4IL/utils/text/mimetypes.txt