I know and i am paranoid that this would be tagged DUPLICATE
However i am stuck at something which i cannot resolve myself so i need your help.
Basically i abstracted concept of reading first 8 bytes from the Image(any) and depending on that decide if it falls under any of types(PNG,JPEG,GIF) .
I am trying to acheive this in Java.
package examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import org.apache.commons.io.IOUtils;
public class BlobCheck
{
public static void main(String args[]) throws Exception
{
File dir = new File(args[0]);
File files[] = dir.listFiles();// Here this files will be changed to
// Blobs from database and then i will
// convert each blob to bytes.
StringBuffer sb = new StringBuffer();
StringBuilder chars = new StringBuilder();
File afile[];
int j = (afile = files).length;
for (int i = 0; i < j; i++)
{
File file = afile[i];
FileInputStream fis = new FileInputStream(file);
byte bytearr[] = IOUtils.toByteArray(fis);
long count = 0L;
byte abyte0[];
int l = (abyte0 = bytearr).length;
for (int k = 0; k < l; k++)
{
byte b = abyte0[k];
if (count == 8L)
break;
sb.append(b);
chars.append((char) b);
count++;
}
// if ("-1-40-1-320167470".equals(sb.toString()))
/*
* if ("-1-40-1".equals(sb.toString())) System.out.println((new
* StringBuilder
* (String.valueOf(file.getName()))).append(" is an image file ")
* .append
* (sb.toString()).append(" ").append(chars.toString()).toString());
* else
*/
System.out.println((new StringBuilder(String.valueOf(file.getName()))).append(" ").append(sb.toString()));
sb.delete(0, sb.length());
chars.delete(0, chars.length());
}
}
}
Now,i fill a folder with bunch of different types of files (images,docs,xls,etc..) and excute the class i get the following output.
Here in this,the first 8 byte(decimal) values are different from what has been given in the DUPLICATE (above).Suprisingly most of the images are having same 8 bytes and few are not(highlighted).
Output:
- 2.jpg -1-40-1-320167470
- 2g.gif -1-40-1-320167470
- 324.png -1-40-1-320167470
- 4.jpg -1-40-1-320167470
- 6.jpg -1-40-1-320167470
- 9.jpg -1-40-1-320167470
- Logo.jpg -1-40-1-1801465100
- Lpng.png -1-40-1-1801465100
- picture.xls -48-4917-32-95-7926-31
- Thumbs.db -48-4917-32-95-7926-31
Please let me know if i have gone wrong somewhere! Thanks.