`

辨别文件的真实类型

阅读更多

参考博客http://blog.csdn.net/shixing_11/article/details/5708145

每个文件类型都对应着一个编码头部

下面这些是已知的文件头部[自定义的枚举类型]

package org.masque.file;
/**
 * 
 * Description:文件对应的文件头,资源来自网络
 * FileHead.java Create on 2014年6月22日 下午1:14:03 
 * @author masque.java@outlook.com
 * @version 1.0
 * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 */
public enum FileHead {

	 JPG("jpg", "FFD8FF"), //JPEG (jpg)  
     PNG("png", "89504E47"),  //PNG (png)  
     GIF("gif", "47494638"),  //GIF (gif)  
     TIF("tif", "49492A00"),  //TIFF (tif)  
     BMP("bmp", "424D"), //Windows Bitmap (bmp)  
     DWG("dwg", "41433130"), //CAD (dwg)  
     HTML("html", "68746D6C3E"),  //HTML (html)  
     RTF("rtf", "7B5C727466"),  //Rich Text Format (rtf)  
     XML("xml", "3C3F786D6C"),  
     ZIP("zip", "504B0304"),  
     RAR("rar", "52617221"),  
     PSD("psd", "38425053"),  //Photoshop (psd)  
     EML("eml", "44656C69766572792D646174653A"),  //Email [thorough only] (eml)  
     DBX("dbx", "CFAD12FEC5FD746F"),  //Outlook Express (dbx)  
     PST("pst", "2142444E"),  //Outlook (pst)  
     DOC_XLS("doc_xls", "D0CF11E0"),  //MS Excel 注意:word 和 excel的文件头一样  
     MDB("mdb", "5374616E64617264204A"),  //MS Access (mdb)  
     WPD("wpd", "FF575043"), //WordPerfect (wpd)   
     EPS("eps", "252150532D41646F6265"),  
     PS("ps", "252150532D41646F6265"),  
     PDF("pdf", "255044462D312E"),  //Adobe Acrobat (pdf)  
     QDF("qdf", "AC9EBD8F"),  //Quicken (qdf)  
     PWL("pwl", "E3828596"),  //Windows Password (pwl)  
     WAV("wav", "57415645"),  //Wave (wav)  
     AVI("avi", "41564920"),  
     RAM("ram", "2E7261FD"),  //Real Audio (ram)  
     RM("rm", "2E524D46"), //Real Media (rm)  
     MPG("mpg", "000001BA"),  //  
     MOV("mov", "6D6F6F76"),  //Quicktime (mov)  
     ASF("asf", "3026B2758E66CF11"), //Windows Media (asf)  
     MID("mid", "4D546864");  //MIDI (mid)  
     
     private String type;
     
     private String code;
     
     FileHead(String type,String code){
    	 this.type = type;
    	 this.code = code;
     }

	public String getType() {
		return type;
	}

	public String getCode() {
		return code;
	}
	
	public static FileHead getLikeCode(String code){
		System.out.println(code);
		if (code==null||"".equals(code)) 
			return null;
		code = code.toUpperCase();
		FileHead [] fhs = FileHead.values();
		for (FileHead fh:fhs) {
			//模糊匹配文件头
			if (code.startsWith(fh.code)) {
				return fh;
			}
		}
		return null;
	}
	
	public static FileHead getByType(String type){
		FileHead [] fhs = FileHead.values();
		for (FileHead fh:fhs) {
			if (fh.getType().equals(type)) {
				return fh;
			}
		}
		return null;
	}
	
	public static FileHead getByCode(String code){
		FileHead [] fhs = FileHead.values();
		for (FileHead fh:fhs) {
			if (fh.getCode().equals(code)) {
				return fh;
			}
		}
		return null;
	}
}

 使用方法如下

package org.masque.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
 * 
 * Description: 读取文件的前部分字节在已知的库中比对
 * CheckFileHead.java Create on 2014年6月22日 下午1:48:09 
 * @author masque.java@outlook.com
 * @version 1.0
 * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 */
public class CheckFileHead {

	public static void main(String[] args) {
		String fileHeadStr = getFileByFile(new File("C:/40882199905262314.jpg"));
		FileHead fileHead = FileHead.getLikeCode(fileHeadStr);
		System.out.println(fileHead!=null?fileHead.getType():"未知的文件类型!");
	}
	
	public final static String getFileByFile(File file)    
    {    
        String filetype = null;    
        byte[] b = new byte[30];    
        try    
        {    
            InputStream is = new FileInputStream(file);    
            is.read(b);    
            filetype = getFileHexString(b);
            is.close();    
        }    
        catch (FileNotFoundException e)    
        {    
            e.printStackTrace();    
        }    
        catch (IOException e)    
        {    
            e.printStackTrace();    
        }    
        return filetype;    
    }    
	
	public final static String getFileHexString(byte[] b)    
    {    
        StringBuilder stringBuilder = new StringBuilder();    
        if (b == null || b.length <= 0)    
        {    
            return null;    
        }    
        for (int i = 0; i < b.length; i++)    
        {    
            int v = b[i] & 0xFF;    
            String hv = Integer.toHexString(v);    
            if (hv.length() < 2)    
            {    
                stringBuilder.append(0);    
            }    
            stringBuilder.append(hv);    
        }    
        return stringBuilder.toString();    
    }    
}

 代码比较简单就不做详细介绍

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics