Detecting mime-type of a MySQL BLOB in Java -


yes know shouldn't store images on database, thanks.

that said, there way detect in java mime type of blob stored in mysql?

it images (.gif, .png, .jpeg, etc.) don't need common purpose tool.

thanks lot guys

bonus points if proposed solution not involve 3rd party libs :)

i imagine can take @ headers. need read data byte array , examine bytes see if match headers different file-types.

for example, gif, first 3 bytes "gif" (4716 4916 4616) followed either "87a" (3816 3716 6116) or "89a" (3816 3916 6116).

so should work (uses fileinputstream demonstration purposes, can binary data blob using resultset#getbinarystream(int) or resultset#getbinarystream(string)):

import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.util.arrays;  public class identifyimage {     public static void main(string[] args) throws ioexception {         fileinputstream in = null;          try {             in = new fileinputstream("sample.gif");              //the following in base 10             byte[] gifheader87a = {71, 73, 70, 56, 55, 97};             byte[] gifheader89a = {71, 73, 70, 56, 57, 97};              byte[] bytes = new byte[6];             in.read(bytes, 0, 6);              if(arrays.equals(gifheader89a, bytes) || arrays.equals(gifheader87a, bytes)) {                system.out.println("it's gif!");             }          } {             if (in != null) {                 in.close();             }         }     } } 

you need bytes in header other file types (like jpg, png, etc.). not sure if best way. may have better solution.

as far 3rd-party libraries, jmimemagic pretty good. easiest solution :).

edit

i found this article here lists header information different file types. there sample code, it's written in language (clarion).


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -