https://blog.csdn.net/adsadadaddadasda/article/details/83047344
package com.example.test;
import org.junit.Test;
import java.io.File;
/**
* @ClassName TestFileFormatVerify
* @Description 测试FileFormatVerify类
* @Author sun
* Date 2019/5/8 14:57
* @Version 1.0
**/
public class TestFileFormatVerify {
/**
* @Description 测试传入参数后缀是否有篡改
* @Param []
* @return void
**/
@Test
public void TestSuffixReg(){
FileFormatVerify reg = new FileFormatVerify();
File file = new File("E:\\ideaFile\\RegEx\\src\\1.xlsx");
if (file.isFile() && file.exists()){
boolean b = reg.suffixVerify(file);
System.out.println(b);
}
}
/**
* @Description 查看传入文件格式的前几位字符
* @Param []
* @return void
**/
@Test
public void CheckFileByte8(){
FileFormatVerify reg = new FileFormatVerify();
File file = new File("E:\\ideaFile\\RegEx\\src\\1.xlsx");
byte[] bytes = reg.inputStream2ByteArray(file);
String s = reg.bytesToHexString(bytes);
System.out.println(s);
}
}
package com.example.test;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName FileFormatVerify
* @Description 图片 pdf/wored/excel和各种压缩格式的校验
**/
public class FileFormatVerify {
private static Map<String, Object> fileFormat = null;
//允许上传的文件类型
private String fileTypeAll = "mp3,mp4,video,rmvb,pdf,txt,xml,doc,gif,png,bmp,jpeg";
//允许上传的文件最大大小(100M,单位为byte)
private Long maxSize = Long.parseLong(String.valueOf(1024 * 1024 * 100));
/**
* @return
* @Description 将常用需要校验的格式和区别其他格式的前几个字节写入map
* @Param []
**/
public FileFormatVerify() {
fileFormat = new HashMap<String, Object>();
//JPEG
fileFormat.put("jpg", "FFD8FFE0");
//PNG
fileFormat.put("png", "89504E47");
fileFormat.put("zip", "504B0304");
fileFormat.put("rar", "52617221");
/**
* docx ,xlsx和zip 相同 doc 和 xls 相同
*/
fileFormat.put("docx", "504b0304");
fileFormat.put("doc", "d0cf11e0");
fileFormat.put("xls", "d0cf11e0");
fileFormat.put("xlsx", "504b0304");
//fileFormat.put("pdf","255044462D312E");
fileFormat.put("pdf", "255044462");
}
/**
* @return boolean 返回true 表示文件格式验证通过, 返回false 文件格式验证失败
* @Description 根据传入的文件获得后缀, 获得指定文件格式byte[]数组中的前8位字符
* 将传入文件转化为byte[]数组,取前8位.判断传入文件的前8位和我们指定好的文件byte[]的前8位是否相同,
* 如果相同则文件格式没有被篡改,反之,文件后缀格式被篡改
* @Param [file]
**/
public boolean suffixVerify(File file) {
String fileType = "";
String name = file.getName();
int i = name.lastIndexOf(".");
// 获取文件的后缀
if (i > 0) {
fileType = name.substring(i + 1);
}
//根据文件的后缀获取,获取文件的byte[]的前8位
if (fileFormat.containsKey(fileType.toLowerCase())) {
String fileByte8 = String.valueOf(fileFormat.get(fileType.toLowerCase()));
//获取传入文件的byte[]的前8位
byte[] bytes = inputStream2ByteArray(file);
String compareByte = bytesToHexString(bytes);
//如果传入文件的byte[]的前8位和我们定义好的byte[]的前8位相同,验证通过.
if (compareByte.startsWith(fileByte8)) {
//如果格式校验成功
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* @return byte[]
* @Description 将file文件转化为byte[]
* @Param [file]
**/
public byte[] inputStream2ByteArray(File file) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileInputStream fis = null;
byte[] buffer = null;
try {
fis = new FileInputStream(file);
//不用读取全部文件,只读文件前面的部分
byte[] b = new byte[1024];
fis.read(b);
bos.write(b, 0, 1024);
/**
byte[] b = new byte[4];
int n;
while ((n = fis.read(b)) != -1){
bos.write(b, 0, n);
}
*/
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (bos != null) {
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
/**
* @return java.lang.String
* @Description 取byte[]前8位的为字符串
* @Param [src]
**/
public String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
//return stringBuilder.toString().substring(0, 8);
return stringBuilder.toString();
}
/**
* 根据java.nio.*的流获取文件大小
*
* @param file
*/
public Long getFileSize(File file) {
FileChannel fc = null;
Long size = null;
try {
if (file.exists() && file.isFile()) {
String fileName = file.getName();
FileInputStream fis = new FileInputStream(file);
fc = fis.getChannel();
System.out.println("文件" + fileName + "的大小是:" + fc.size() + "\n");
size = fc.size();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fc) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return size;
}
/**
* 校验文件格式是否支持和大小是否符合检验
* @param file
* @return true 符合
*/
public boolean check(File file) {
boolean flag = false;
String fileType = "";
String name = file.getName();
//judge file type
if ( Arrays.asList(fileType.split(",")).contains(name)){
int i = name.lastIndexOf(".");
// 获取文件的后缀
if (i > 0) {
fileType = name.substring(i + 1);
}
//根据文件的后缀获取,获取文件的byte[]的前8位
if (fileFormat.containsKey(fileType.toLowerCase())) {
String fileByte8 = String.valueOf(fileFormat.get(fileType.toLowerCase()));
//获取传入文件的byte[]的前8位
byte[] bytes = inputStream2ByteArray(file);
String compareByte = bytesToHexString(bytes);
Long size = getFileSize(file);
//如果传入文件的byte[]的前8位和我们定义好的byte[]的前8位相同,验证通过.
if (compareByte.startsWith(fileByte8) && maxSize > size) {
//如果格式校验成功
flag = true;
}
}
}
return flag;
}
public void fileToMultipartFile() {
}
}
网友评论