add:检查文件是否为加密文件的判断代码逻辑

This commit is contained in:
zyd
2022-10-12 22:58:48 +08:00
parent 8f74bbfbd9
commit c266ddc4a1
7 changed files with 289 additions and 4 deletions
@@ -0,0 +1,127 @@
package com.adc.da.utils.util;
import com.adc.da.att.entity.AttFileEO;
import com.adc.da.util.UUIDUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.List;
/**
* 加密文件判断工具类
*/
@Component
public class EncryptFileUtils {
private static final Logger logger = LoggerFactory.getLogger(EncryptFileUtils.class);
@Value("${file.path}")
private String filePath;//文件存储路径
public static final String ENCRYPT_FILE_HEX ="18 1B 03 1A 15 10 19 7C 0A 19 0E 7C 6F 72 6C 5F 5C 5C 5C 5C 5C 5C 5C C3 AD E5 AF 5C 5C 5C 5C 5C 8F EF EF 80 B5 AE 85 B3 94 86 A7 8C 86 E3 A2 FC B5 B4 83 8A B7 8B A9 87 F2 F1 BC F5 8E BF 90 E3 A4 86 8F 8B 94 FC A7 90 A4 EF 87 BC B6 8F 89 B1 F4 8E F7 8D 8A B5 8B 9D F3 89 AA 80 B1 90 82 8C 9F B7 85 A9 B0 89 B2 96 B3 B0 A6 B7 B5 8F 93 E1 AD BD E6 AE 8F 95 92 97 8D F0 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 C5 ";
public String checkFileIsEncryFile(AttFileEO fileEO){
if(fileEO!=null){
String readFilePath = filePath+fileEO.getFilePath()+fileEO.getFileName();
boolean readFlag = true;
byte[] b = new byte[160];
InputStream inputStream = null;
try {
inputStream = new FileInputStream(readFilePath);
inputStream.read(b, 0, 160);
} catch (IOException e) {
logger.error(e.getMessage(),e);
readFlag = false;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error(e.getMessage(),e);
}
}
}
String hexString = bytesToHexString(b);
if(readFlag){
if(StringUtils.isNotBlank(hexString)){
if(StringUtils.equals(hexString,ENCRYPT_FILE_HEX)){
return "ENCRYPT";
}else{
return "UNENCRYPT";
}
}else{
return "READNO";
}
}else{
return "READERR";
}
}else{
return "NOFILE";
}
}
/**
* 将文件头转换成16进制字符串
*
* @param 原生byte
* @return 16进制字符串
*/
private static 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.toUpperCase()).append(" ");
}
return stringBuilder.toString();
}
/**
* 本地写TXT文件
* @param path
* @param list
*/
public void writeTxt(List<String> list) {
String savePath = filePath+"/TEMPPATH/"+ UUIDUtils.randomSnowflakeID()+".txt";
BufferedWriter bw = null;
FileWriter fr = null;
try {
//将写入转化为流的形式
fr = new FileWriter(savePath);
bw = new BufferedWriter(fr);
//一次写一行
for (String s : list) {
bw.write(s);
bw.newLine(); //换行用
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fr != null) {
fr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}