Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.util.HashMap; /** * Obtain and determine file header information * * @author Sud * */public class Gettypebyhead {//cache header information-File header information public static final HASHMAP&L T
String, string> mfiletypes = new hashmap<string, string> ();
static {//Images mfiletypes.put ("ffd8ff", "jpg");
Mfiletypes.put ("89504E47", "PNG");
Mfiletypes.put ("47494638", "gif");
Mfiletypes.put ("49492a00", "TIF");
Mfiletypes.put ("424D", "BMP"); Mfiletypes.put ("41433130", "DWG");
CAD mfiletypes.put ("38425053", "PSD"); Mfiletypes.put ("7b5c727466", "RTF");
Journal mfiletypes.put ("3c3f786d6c", "xml");
Mfiletypes.put ("68746d6c3e", "html"); Mfiletypes.put ("44656c69766572792d646174653a", "eml");
Mail Mfiletypes.put ("D0cf11e0", "Doc");
Mfiletypes.put ("5374616e64617264204a", "MDB");
Mfiletypes.put ("252150532d41646f6265", "PS");
Mfiletypes.put ("255044462d312e", "pdf");
Mfiletypes.put ("504b0304", "docx"); Mfiletypes.put ("52617221","RAR");
Mfiletypes.put ("57415645", "wav");
Mfiletypes.put ("41564920", "avi");
Mfiletypes.put ("2e524d46", "rm");
Mfiletypes.put ("000001BA", "mpg");
Mfiletypes.put ("000001b3", "mpg");
Mfiletypes.put ("6d6f6f76", "mov");
Mfiletypes.put ("3026b2758e66cf11", "ASF");
Mfiletypes.put ("4d546864", "mid");
Mfiletypes.put ("1f8b08", "GZ");
Mfiletypes.put ("4d5a9000", "Exe/dll");
Mfiletypes.put ("75736167", "txt"); /** * Get header information from file path * * @param filePath * File path * @return file header information/public static String Getfil
EType (String filePath) {System.out.println (GetFileHeader (FilePath));
System.out.println (Mfiletypes.get (GetFileHeader (FilePath)));
Return Mfiletypes.get (GetFileHeader (FilePath)); /** * Get header information from file path * * @param filePath * File path * @return file header information/public static String Getfil
Eheader (String filePath) {FileInputStream is = null;
String value = null;
try {is = new FileInputStream (FilePath); Byte[] B = new byTE[4]; /* int read () reads a byte of data from this input stream. int read (byte[] b) reads the most b.length bytes of data from this input stream into a byte array.
int read (byte[] b, int off, int len) * Read the maximum Len byte of data from the input stream into a byte array.
* * Is.read (b, 0, b.length);
Value = bytestohexstring (b);
catch (Exception e) {} finally {if (null!= is) {try {is.close ();
The catch (IOException e) {}} return value;
/** * Converts the byte array of the file that will read the header information to string representing * * @param src * byte array of files to read file header information * @return File header information * *
private static String bytestohexstring (byte[] src) {StringBuilder builder = new StringBuilder ();
if (src = null | | | src.length <= 0) {return null;
} String HV; for (int i = 0; i < src.length; i++) {//returns the string representation of an integer parameter in 16-in (Cardinal 16) unsigned integers and converts to uppercase HV = integer.tohexstring (src
[i] & 0xFF). toUpperCase ();
if (Hv.length () < 2) {builder.append (0);
} builder.append (HV);
} System.out.println (Builder.tostring ()); return buildEr.tostring ();
public static void Main (string[] args) throws Exception {final String fileType = Getfiletype ("D:\\ry4s_java.dll");
System.out.println (FileType); }
}