From 5379eb2945d4fbdeaa176ca87c62e6269d893d6c Mon Sep 17 00:00:00 2001 From: luyie Date: Wed, 21 Aug 2024 09:29:02 +0800 Subject: [PATCH] =?UTF-8?q?add:=E8=A7=86=E9=A2=91=E5=B7=A1=E6=A3=80?= =?UTF-8?q?=EF=BC=8C=E5=A4=A7=E6=A8=A1=E5=9E=8B=E8=AF=86=E5=88=AB=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hnac/hzims/common/utils/FileUtil.java | 74 +++++++--------------- .../service/impl/ZhipuAnalysisServiceImpl.java | 7 +- .../inspect/task/schedule/VideoTaskSchedule.java | 61 ++++++++++-------- 3 files changed, 66 insertions(+), 76 deletions(-) diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java index ce65fb3..180eb0e 100644 --- a/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java @@ -6,7 +6,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.multipart.MultipartFile; import java.io.*; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.util.Base64; @@ -17,7 +16,7 @@ import java.util.Base64; */ @Slf4j public class FileUtil { - public static byte[] getFileBytes(String filePath) { + public static byte[] getFileBytes(String filePath) throws IOException { File file = new File(filePath); FileInputStream fileInputStream = null; byte[] bytes = null; @@ -25,21 +24,15 @@ public class FileUtil { fileInputStream = new FileInputStream(file); bytes = new byte[(int) file.length()]; fileInputStream.read(bytes); - return bytes; - } catch (Exception e) { - log.error("读取文件出错", e); } finally { if (null != fileInputStream) { - try { - fileInputStream.close(); - } catch (IOException ignore) { - } + fileInputStream.close(); } } return bytes; } - public static byte[] getUrlByte(URL url) { + public static byte[] getUrlByte(URL url) throws IOException { InputStream inputStream = null; ByteArrayOutputStream outputStream = null; byte[] fileBytes = null; @@ -52,30 +45,19 @@ public class FileUtil { outputStream.write(buffer, 0, bytesRead); } fileBytes = outputStream.toByteArray(); - } catch (Exception e) { - log.error("获取url文件失败", e); } finally { - try { - if (null != inputStream) { - inputStream.close(); - } - if (null != outputStream) { - outputStream.close(); - } - } catch (Exception ignore) { - + if (null != inputStream) { + inputStream.close(); + } + if (null != outputStream) { + outputStream.close(); } } return fileBytes; } - public static byte[] getUrlByte(String urlString) { - try { - return getUrlByte(new URL(urlString)); - } catch (MalformedURLException e) { - log.error("获取url文件失败", e); - } - return null; + public static byte[] getUrlByte(String urlString) throws IOException { + return getUrlByte(new URL(urlString)); } public static String getContentTypeFromUrl(String url) { @@ -113,16 +95,12 @@ public class FileUtil { return result; } - public static String getPureUrl(String url) { + public static String getPureUrl(String url) throws UnsupportedEncodingException { String result = removeUrlParam(url); - try { - result = URLDecoder.decode(result, "utf-8"); - int index = result.lastIndexOf("http"); - if (index > 0) { - result = result.substring(index); - } - } catch (UnsupportedEncodingException e) { - log.error("解码url失败"); + result = URLDecoder.decode(result, "utf-8"); + int index = result.lastIndexOf("http"); + if (index > 0) { + result = result.substring(index); } return result; } @@ -144,23 +122,19 @@ public class FileUtil { return url; } - public static MultipartFile getMultipartFileFromUrl(String urlStr) { + public static MultipartFile getMultipartFileFromUrl(String urlStr) throws IOException { String pureUrl = getPureUrl(urlStr); return getMultipartFileFromPureUrl(pureUrl); } - public static MultipartFile getMultipartFileFromPureUrl(String pureUrlStr) { + public static MultipartFile getMultipartFileFromPureUrl(String pureUrlStr) throws IOException { MultipartFile result = null; - try { - URL url = new URL(pureUrlStr); - byte[] bytes = getUrlByte(url); - String name = url.getPath(); - String originalName = getOriginalFilename(name); - String contentType = getContentTypeFromUrl(originalName); - result = new ByteArrayMultipartFile(name, originalName, contentType, bytes); - } catch (Exception e) { - log.error("获取url文件失败", e); - } + URL url = new URL(pureUrlStr); + byte[] bytes = getUrlByte(url); + String name = url.getPath(); + String originalName = getOriginalFilename(name); + String contentType = getContentTypeFromUrl(originalName); + result = new ByteArrayMultipartFile(name, originalName, contentType, bytes); return result; } @@ -168,7 +142,7 @@ public class FileUtil { return Base64.getEncoder().encodeToString(bytes); } - public static String getBase64(String filePath) { + public static String getBase64(String filePath) throws IOException { return getBase64(getFileBytes(filePath)); } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java index f899713..d3b9a78 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java @@ -25,6 +25,7 @@ import org.springblade.core.tool.utils.StringUtil; import org.springframework.stereotype.Service; import org.springframework.util.Assert; +import java.io.IOException; import java.util.*; /** @@ -100,7 +101,11 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { return request.getUrl(); } if (StringUtil.isNotBlank(request.getFilePath())) { - return FileUtil.getBase64(request.getFilePath()); + try { + return FileUtil.getBase64(request.getFilePath()); + } catch (IOException e) { + throw new HzServiceException("获取文件内容失败"); + } } return null; } diff --git a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java index f81601b..ddf145e 100644 --- a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java +++ b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java @@ -11,6 +11,7 @@ import com.hnac.hzims.bigmodel.api.feign.IBigModelAnalysisClient; import com.hnac.hzims.common.utils.FileUtil; import com.hnac.hzinfo.datasearch.soe.ISoeClient; import com.hnac.hzinfo.datasearch.soe.vo.DeviceSoeVO; +import com.hnac.hzinfo.exception.HzServiceException; import com.hnac.hzinfo.inspect.ai.entity.CameraInfoEntity; import com.hnac.hzinfo.inspect.ai.entity.RobotTaskEntity; import com.hnac.hzinfo.inspect.ai.service.ICameraInfoService; @@ -54,6 +55,7 @@ import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; @@ -241,7 +243,14 @@ public class VideoTaskSchedule { } private void pictureHandle(String url, Long contentId, EventRecordEntity record, TaskEntity task) { - MultipartFile multipartFile = FileUtil.getMultipartFileFromUrl(url); + + MultipartFile multipartFile = null; + try { + multipartFile = FileUtil.getMultipartFileFromUrl(url); + } catch (IOException e) { + log.error("获取截图文件失败", e); + throw new HzServiceException("获取截图文件失败"); + } R fileResponse = ossClient.putFile(multipartFile); if (fileResponse.isSuccess()) { record.setFileUrl(Optional.ofNullable(fileResponse.getData().getLink()).orElse(record.getFileUrl())); @@ -252,33 +261,35 @@ public class VideoTaskSchedule { return; } + String[] typeArray = content.getMultiCheckTypeSon().split(","); + List checkTypeSonList = Arrays.asList(typeArray); + String text = null; try { - String[] typeArray = content.getMultiCheckTypeSon().split(","); - List checkTypeSonList = Arrays.asList(typeArray); - String text = FileUtil.getBase64(multipartFile.getBytes()); - BigModelAnalysisRequestDTO analysisRequest = new BigModelAnalysisRequestDTO(); - analysisRequest.setCheckTypeSonList(checkTypeSonList); - analysisRequest.setUrl(text); - BigModelAnalysisResponseDTO analysisResponse = bigModelClient.analysis(analysisRequest); - if (!analysisResponse.getSuccess() || CollectionUtil.isEmpty(analysisResponse.getData())) { - return; - } - List results = analysisResponse.getData(); - StringBuilder stringBuilder = new StringBuilder(); - Boolean status = Boolean.TRUE; - for (BigModelAnalysisResponseDTO.BigModelAnalysisResult result : results) { - stringBuilder.append(result.getDescription()).append("\n"); - Boolean isGood = (Boolean) result.getResult(); - status = status && isGood; - if (!isGood) { - sendMessage(result.getMessage(), task, content); - } + text = FileUtil.getBase64(multipartFile.getBytes()); + } catch (IOException e) { + log.error("转换截图文件内容失败", e); + throw new HzServiceException("转换截图文件内容失败"); + } + BigModelAnalysisRequestDTO analysisRequest = new BigModelAnalysisRequestDTO(); + analysisRequest.setCheckTypeSonList(checkTypeSonList); + analysisRequest.setUrl(text); + BigModelAnalysisResponseDTO analysisResponse = bigModelClient.analysis(analysisRequest); + if (!analysisResponse.getSuccess() || CollectionUtil.isEmpty(analysisResponse.getData())) { + throw new HzServiceException("大模型识别失败"); + } + List results = analysisResponse.getData(); + StringBuilder stringBuilder = new StringBuilder(); + Boolean status = Boolean.TRUE; + for (BigModelAnalysisResponseDTO.BigModelAnalysisResult result : results) { + stringBuilder.append(result.getDescription()).append("\n"); + Boolean isGood = (Boolean) result.getResult(); + status = status && isGood; + if (!isGood) { + sendMessage(result.getMessage(), task, content); } - record.setCurrentStatus(status ? "1" : "0"); - record.setCurrentValue(stringBuilder.toString()); - } catch (Exception e) { - log.error("video task get big model analysis result fail", e); } + record.setCurrentStatus(status ? "1" : "0"); + record.setCurrentValue(stringBuilder.toString()); } private void sendMessage(String messageText, TaskEntity task, ContentEntity content) {