I have file content which is currently as byte[]
and I want to know it's mime-type / content-type header.
The issue is that, I saw many examples over the web to find it but only when the file is actually exists.
How can I get this info by only this byte[]
.
Most of the codes which I tested are like:
File f = new File("gumby.gif");
System.out.println("Mime Type: " + new MimetypesFileTypeMap().getContentType(f));
EDIT 1:
This code gave me a wrong result which is: application/octet-stream
Is there a way to get this info without creating a file ?
EDIT 2: I tried this code:
public static void main(String[] args) throws Exception{
Tika tika = new Tika();
byte[] content = readFile().getBytes();
System.out.println(tika.detect(content));
}
private static String readFile() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("c:\\pic.jpg"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
return everything;
} finally {
br.close();
}
}
And it's always returns application/octet-stream
, is it because i'm using byte[]
as parameter?