java获取文件的类型的文件_本地文件/url获取文件类型

该Java类用于检查本地文件或URL的文件类型。通过读取文件头的字节来识别常见的文件格式,包括图片、CAD、日记本、邮件、文档、电子表格、数据库、压缩文件、音频、视频等。
摘要由CSDN通过智能技术生成

package com.cjw.baidu.ocr;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;

import java.io.*;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.HashMap;

/**

* 检查文件类型

* @author wucj

* @date 2019年10月16日21:48:03

*/

@Slf4j

public class CheckFileType {

/**

* 缓存文件头信息-文件头信息

*/

public static final HashMap mFileTypes = new HashMap();

static {

// images

mFileTypes.put("FFD8FF", "jpg");

mFileTypes.put("89504E47", "png");

mFileTypes.put("47494638", "gif");

mFileTypes.put("49492A00", "tif");

mFileTypes.put("424D", "bmp");

// CAD

mFileTypes.put("41433130", "dwg");

mFileTypes.put("38425053", "psd");

// 日记本

mFileTypes.put("7B5C727466", "rtf");

mFileTypes.put("3C3F786D6C", "xml");

mFileTypes.put("68746D6C3E", "html");

// 邮件

mFileTypes.put("44656C69766572792D646174653A", "eml");

mFileTypes.put("D0CF11E0", "doc");

//excel2003版本文件

mFileTypes.put("D0CF11E0", "xls");

mFileTypes.put("5374616E64617264204A", "mdb");

mFileTypes.put("252150532D41646F6265", "ps");

mFileTypes.put("255044462D312E", "pdf");

mFileTypes.put("504B0304", "docx");

//excel2007以上版本文件

mFileTypes.put("504B0304", "xlsx");

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");

}

/**

* filePath获取文件的类型

* @param filePath 文件路径

* @return

* @throws FileNotFoundException

*/

public static String getFileType(String filePath) throws FileNotFoundException {

File file = new File(filePath);

if(file.isDirectory()){

throw new RuntimeException("当前路径是目录");

}

return getFileType(new FileInputStream(file));

}

/**

* url获取文件类型

* @param fileUrl 网络文件url

* @return

* @throws Exception

*/

public static String urlFileType(String fileUrl) throws Exception {

log.info("url:{}获取文件类型",fileUrl);

BufferedInputStream bis = null;

HttpURLConnection urlconnection = null;

URL url = null;

url = new URL(fileUrl);

urlconnection = (HttpURLConnection) url.openConnection();

urlconnection.connect();

String fileType = getFileType(urlconnection.getInputStream());

log.info("url:{}获取文件类型:{}",fileUrl,fileType);

return fileType;

}

/**

* @return 文件头信息

* @author liang.pan

*

* 方法描述:根据输入流获取文件头信息

*/

public static String getFileType(InputStream inputStream) {

return mFileTypes.get(getFileHeader(inputStream));

}

/**

* @return 文件头信息

* 方法描述:根据输入流获取文件头信息

*/

public static String getFileHeader(InputStream inputStream) {

InputStream is = null;

String value = null;

try {

is = inputStream;

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) {

}

}

}

if (StringUtils.startsWith(value, "FFD8FF")) {

value = value.substring(0, 6);

}

return value;

}

/**

* @param src 要读取文件头信息的文件的byte数组

* @return 文件头信息

*

* 方法描述:将要读取文件头信息的文件的byte数组转换成string类型表示

*/

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);

}

return builder.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值