It turned out that there is a decent method in JDK
's URLConnection
class, please refer to the following answer: Getting A File's Mime Type In Java
If one needs to extract file extension from byte array instead of file, one should simply use java.io.ByteArrayInputStream
(class to read bytes specifically from byte arrays) instead of java.io.FileInputStream
(class to read bytes specifically from files) like in the following example:
byte[] content = ;
InputStream is = new ByteArrayInputStream(content);
String mimeType = URLConnection.guessContentTypeFromStream(is);
//...close stream
Hope this helps...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…