目录
1.通过路径获取minio文件内容
public static String getFileContent(String fileKey, String bucketName) {
String result = StrUtil.EMPTY;
if (ObjectUtil.isNotEmpty(fileKey)) {
if (!checkPicture(fileKey)) {
throw new RuntimeException("不支持该种文件格式的读写!");
}
try {
InputStream stream = MinIoUtil.getObject(bucketName, fileKey, 0, null);
if (null != stream) {
BufferedInputStream file = new BufferedInputStream(stream);
ByteArrayOutputStream boa = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = file.read(buffer)) != -1) {
boa.write(buffer, 0, len);
}
boa.flush();
InputStream streamCopy = new ByteArrayInputStream(boa.toByteArray());
String encode = CharsetConvertUtil.getEncode(new BufferedInputStream(streamCopy), false);
file.close();
result = boa.toString(encode);
}
} catch (Exception e) {
throw new ServiceException("获取文件" + fileKey + "失败: " + e.getMessage());
}
}
return result;
}
2.将文本内容转成Map
private Map<String, Object> getContent(String sourcePath, String key) {
Map<String, Object> map = new HashMap<>();
String sourceContent = FileOtherUtil.getFileContent(sourcePath, FileSystemConstans.BUCKET_PROJECTS);
String[] dataSourceArray = sourceContent.split("\n|\r");
if (dataSourceArray.length > 0) {
if (StringUtils.isEmpty(key)) {
for (String data : dataSourceArray) {
if (data.contains("=")) {
String[] splitContent = replaceBlank(data).split("=");
map.put(splitContent[0], splitContent[1]);
}
}
} else {
Map<String, Object> inputSourceMap = new HashMap<>();
Map<String, Object> outputSourceMap = new HashMap<>();
for (String data : dataSourceArray) {
data = data.trim();
if ("ATS->VPI输入码位:".equals(data) || "VPI->ATS输出码位:".equals(data) || "信号机总人解时间及引导定时:".equals(data)) {
key = data;
}
if (data.contains("=")) {
String[] splitContent = replaceBlank(data).split("=");
if ("ATS->VPI输入码位:".equals(key)) {
inputSourceMap.put(splitContent[0], splitContent[1]);
}
if ("VPI->ATS输出码位:".equals(key)) {
outputSourceMap.put(splitContent[0], splitContent[1]);
}
}
}
map.put("input", inputSourceMap);
map.put("output", outputSourceMap);
}
}
return map;
}
3.比对map是否相同
private static boolean mapCompare(Map<String, Object> hashMap, Map<String, Object> hashMap2) {
boolean isChange = false;
boolean isSame = hashMap.keySet().equals(hashMap2.keySet());
if (isSame) {
for (Map.Entry<String, Object> entry1 : hashMap.entrySet()) {
String m1value = entry1.getValue() == null ? "" : (String) entry1.getValue();
String m2value = hashMap2.get(entry1.getKey()) == null ? "" : (String) hashMap2.get(entry1.getKey());
if (!m1value.equals(m2value)) {
isChange = true;
}
}
} else {
isChange = true;
}
return isChange;
}
4.输出结果
private void checkTXT(String sourcePath, String inputDestination, String outputDestination, String dataType, List<TldTlvCheckRecord> list, String projectId) {
Map<String, Object> map = getContent(sourcePath, "A");
Map<String, Object> inputSourceMap = (Map<String, Object>) map.get("input");
Map<String, Object> outputSourceMap = (Map<String, Object>) map.get("output");
if (StringUtils.isNotBlank(inputDestination)) {
Map<String, Object> inputDestinationMap = getContent(inputDestination, null);
boolean same = mapCompare(inputSourceMap, inputDestinationMap);
if (same) {
addData(sourcePath, inputDestination, "md5值不同", dataType, list, projectId);
}
}
if (StringUtils.isNotBlank(outputDestination)) {
Map<String, Object> outputDestinationMap = getContent(outputDestination, null);
boolean same = mapCompare(outputSourceMap, outputDestinationMap);
if (same) {
addData(sourcePath, outputDestination, "md5值不同", dataType, list, projectId);
}
}
}
5.补充备注
1.获取minio 文件名
public static List<String> getZipNameList(String bucketName,String prefixName){
List<String> pathList = new ArrayList<>();
long start = System.currentTimeMillis();
if (MinIoUtil.bucketExists(bucketName)) {
try {
Iterable<io.minio.Result<Item>> results = MinIoUtil.getObjByPrefix(bucketName,prefixName,false);
for (io.minio.Result<Item> result : results) {
Item item = result.get();
String objName = item.objectName();
String replaceName = objName.replace(prefixName + "/", "");
if (replaceName.endsWith("/")) {
replaceName = replaceName.replace("/", "");
}
pathList.add(replaceName);
}
} catch (Exception e) {
log.error("文件获取失败,原因:{}", e.getMessage());
throw new ServiceException("系统异常!文件获取失败");
}
}
long end = System.currentTimeMillis();
log.info("获取总耗时,耗时:" + (end - start) + " ms");
return pathList;
}
2.获取所有路径名
public static List<String> getZipPathList(String bucketName,String prefixName){
long start = System.currentTimeMillis();
List<String> pathList = new ArrayList<>();
if (MinIoUtil.bucketExists(bucketName)) {
try {
Iterable<io.minio.Result<Item>> objectNames = MinIoUtil.getObjByPrefix(bucketName,prefixName,true);
for (io.minio.Result<Item> result : objectNames) {
Item item = result.get();
pathList.add(item.objectName());
}
} catch (Exception e) {
log.error("文件获取失败,原因:{}", e.getMessage());
throw new ServiceException("系统异常!文件获取失败");
}
}
long end = System.currentTimeMillis();
log.info("获取总耗时,耗时:" + (end - start) + " ms");
return pathList;
}
3.判断hex5是否相同
public static String checkMd5Hex(String fileKey, String bucketName) {
String result = StrUtil.EMPTY;
if (ObjectUtil.isNotEmpty(fileKey)) {
try {
InputStream stream = MinIoUtil.getObject(bucketName, fileKey, 0, null);
if (null != stream) {
result = DigestUtils.md5Hex(stream);
}
} catch (Exception e) {
throw new ServiceException("获取文件" + fileKey + "失败: " + e.getMessage());
}
}
return result;
}