To detect real file type based on file content(rather than extension) I use apache Tika.
I wrote following code:
InputStream theInputStream = new FileInputStream("D:\\video.mp4");
try (InputStream is = theInputStream;
BufferedInputStream bis = new BufferedInputStream(is);) {
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
MediaType mediaType = detector.detect(bis, md);
mediaType.getBaseType().compareTo(MediaType))
System.out.println(mediaType);
}
this code outputs image/jpeg
.
It is truth because I changed file extension.
Now I want to check that file is image.
I cannot find enum in MediaType class.
Now I know only the following way:
mediaType.toString().startsWith("image");
But this code looks ugly.
Can you advise nicer solution?