Java: guess content type of byte array
Maybe it is a not a common usecase, but I've just had need to know the content type of a bare byte[].
...
After been searching the whole Internet and evaluated dozens of utility libraries I finally ended up with a couple of very simple but effective solutions.
First solution, 100% JDK-only, based on URLConnection.
Code:
byte[] value = ...; | |
String contentType = null; | |
try { | |
contentType = URLConnection.guessContentTypeFromStream( | |
new ByteArrayInputStream(value)); | |
} catch (IOException e) { | |
LOG.error("Could not guess content type", e); | |
} |
Second solution, suggested by Jukka Zitting, based on Apache Tika with a very small footprint (~450 KB)
Code:
byte[] value = ...; | |
String contentType = new Tika().detect(value); |

3 comments
import org.apache.tika.Tika;
byte[] value = ...;
String contentType = new Tika().detect(value);
if ("application/octet-stream".equals(contentType)) {
LOG.error("Could not guess content type");
}
It's a bit more heavy-weight than the plain JDK (you need the 450kB tika-core jar as a dependency), but covers a much wider range of content types than the JDK.
http://256.com/sources/simplemagic/