当前位置: 移动技术网 > IT编程>开发语言>Java > java如何实现判断文件的真实类型

java如何实现判断文件的真实类型

2019年07月19日  | 移动技术网IT编程  | 我要评论

在文件传输过程中,为了安全验证,对于手工改动文件后缀名产生的伪造文件进行判断过滤。

比如,我们需要的是excel文件,如果不加验证内容,将一些可执行的文件通过更改后缀传输给你,就是一个很大的漏洞了。

java判断文件真实类型依靠的是文件的头部编码信息,具体代码如下:

package com.zhuifeng.util.excel; 
 
import java.io.fileinputstream; 
import java.io.ioexception; 
import java.util.hashmap; 
 
/** 
 * @author guoxk 
 * 
 * 类描述:获取和判断文件头信息 
 *  |--文件头是位于文件开头的一段承担一定任务的数据,一般都在开头的部分。 
 *  |--头文件作为一种包含功能函数、数据接口声明的载体文件,用于保存程序的声明(declaration),而定义文件用于保存程序的实现(implementation)。 
 *  |--为了解决在用户上传文件的时候在服务器端判断文件类型的问题,故用获取文件头的方式,直接读取文件的前几个字节,来判断上传文件是否符合格式。 
 * 
 */ 
public class checkexcelfiletypeutil { 
  // 缓存文件头信息-文件头信息 
  public static final hashmap<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"); // 日记本 
    mfiletypes.put("3c3f786d6c", "xml"); 
    mfiletypes.put("68746d6c3e", "html"); 
    mfiletypes.put("44656c69766572792d646174653a", "eml"); // 邮件 
    mfiletypes.put("d0cf11e0", "doc"); 
    mfiletypes.put("d0cf11e0", "xls");//excel2003版本文件 
    mfiletypes.put("5374616e64617264204a", "mdb"); 
    mfiletypes.put("252150532d41646f6265", "ps"); 
    mfiletypes.put("255044462d312e", "pdf"); 
    mfiletypes.put("504b0304", "docx"); 
    mfiletypes.put("504b0304", "xlsx");//excel2007以上版本文件 
    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"); 
  } 
 
  /** 
   * @author guoxk 
   * 
   * 方法描述:根据文件路径获取文件头信息 
   * @param filepath 文件路径 
   * @return 文件头信息 
   */ 
  public static string getfiletype(string filepath) { 
//   system.out.println(getfileheader(filepath)); 
//   system.out.println(mfiletypes.get(getfileheader(filepath))); 
    return mfiletypes.get(getfileheader(filepath)); 
  } 
 
  /** 
   * @author guoxk 
   * 
   * 方法描述:根据文件路径获取文件头信息 
   * @param filepath 文件路径 
   * @return 文件头信息 
   */ 
  public static string getfileheader(string filepath) { 
    fileinputstream is = null; 
    string value = null; 
    try { 
      is = new fileinputstream(filepath); 
      byte[] b = new byte[4]; 
      /* 
       * int read() 从此输入流中读取一个数据字节。int read(byte[] b) 从此输入流中将最多 b.length 
       * 个字节的数据读入一个 byte 数组中。 int read(byte[] b, int off, int len) 
       * 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。 
       */ 
      is.read(b, 0, b.length); 
      value = bytestohexstring(b); 
    } catch (exception e) { 
    } finally { 
      if (null != is) { 
        try { 
          is.close(); 
        } catch (ioexception e) { 
        } 
      } 
    } 
    return value; 
  } 
 
  /** 
   * @author guoxk 
   * 
   * 方法描述:将要读取文件头信息的文件的byte数组转换成string类型表示 
   * @param src 要读取文件头信息的文件的byte数组 
   * @return  文件头信息 
   */ 
  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++) { 
      // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写 
      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(); 
  } 
  /** 
   * @author guoxk 
   * 
   * 方法描述:测试 
   * @param args 
   * @throws exception 
   */ 
  public static void main(string[] args) throws exception { 
    final string filetype = getfiletype("e:\\补贴名单.xls"); 
    system.out.println(filetype); 
  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网