废话不多说系列,开整~
一、两种检测文件类型的方法
(1)解析文件的十六进制判断
package edu.study.module.up.util.file;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
/**
* 文件处理工具类
*/
public class GetFileTypeUtil {
/**
* 缓存文件头信息-文件头信息
*/
public static final HashMap<String, String> 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");
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");
}
/**
* 根据文件路径获取文件头信息
*
* @param filePath 文件路径
* @return 文件头信息
*/
public static String getFileType(String filePath) {
// System.out.println("文件头信息(文件的十六进制码):" + getFileHeader(filePath));
// System.out.println("文件头信息(文件的十六进制码)2:" + mFileTypes.get(getFileHeader(filePath)));
return mFileTypes.get(getFileHeader(filePath));
}
/**
* 根据文件路径获取文件头信息
*
* @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) {
// TODO 错误日志
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
// TODO 输出日志
e.printStackTrace();
}
}
}
return value;
}
/**
* 将要读取文件头信息的文件的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 (byte b : src) {
// 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
hv = Integer.toHexString(b & 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 {
String filePath = "C:\\ddd.docx";
System.out.println(getFileType(filePath));
}
}
(2)根据 context-type 判断
private static Map<String, String> showFileTypeMap() {
Map<String, String> map = new HashMap<>(18);
map.put("doc", "application/msword");
map.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
map.put("rtf", "application/rtf");
map.put("xls", "application/vnd.ms-excel");
map.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
map.put("ppt", "application/vnd.ms-powerpoint");
map.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
map.put("pdf", "application/pdf");
map.put("swf", "application/x-shockwave-flash");
map.put("rar", "application/octet-stream");
map.put("zip", "application/x-zip-compressed");
map.put("mp3", "audio/mpeg");
map.put("png", "image/png");
map.put("jpeg", "image/jpeg");
map.put("jpg", "image/jpeg");
map.put("jpe", "image/jpeg");
map.put("txt", "text/plain");
map.put("bmp", "image/jpeg");
map.put("exe", "application/octet-stream");
return map;
}
public static String getFiletype(String type) {
String fileType = "未知";
for (Map.Entry<String, String> entry : showFileTypeMap().entrySet()) {
if (entry.getValue().equals(type)) {
fileType = entry.getKey();
break;
}
}
return fileType;
}
public static String getFiletype2(String type) {
String typeTmp = showFileTypeMap().get(type);
return typeTmp == null || "".equals(typeTmp.trim()) ? "未知" : typeTmp;
}
控制层
@Transactional(rollbackFor = Exception.class)
public void uploadfiles(MultipartFile file) {
// 获取上传的文件类型
String type = FileUtils.getfiletype(file.getContentType());
}
至此,谢谢阅读🙏~