From c6ffbcb3cf9b014c7b99170e5dd1c97317e9c4a5 Mon Sep 17 00:00:00 2001 From: zhouweiyi Date: 星期四, 15 五月 2025 17:46:38 +0800 Subject: [PATCH] feat:pdf文件解析图片和分析图片,上传向量数据库都修改成 成异步处理 --- ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 88 insertions(+), 0 deletions(-) diff --git a/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java b/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java index 7ae1b2b..3c16131 100644 --- a/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java +++ b/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java @@ -11,6 +11,8 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; /** * ZIP鏂囦欢澶勭悊宸ュ叿绫� @@ -92,4 +94,90 @@ } return base64Contents.toArray(new String[0]); } + + /** + * 瑙e帇ZIP鏂囦欢骞惰繑鍥濵ultipartFile鏁扮粍 + * + * @param zipData ZIP鏂囦欢鐨勫瓧鑺傛暟缁� + * @return MultipartFile鏁扮粍 + * @throws IOException 濡傛灉瑙e帇杩囩▼涓彂鐢熼敊璇� + */ + public static MultipartFile[] unzipToMultipartFiles(byte[] zipData) throws IOException { + List<MultipartFile> multipartFiles = new ArrayList<>(); + try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData); + ZipInputStream zis = new ZipInputStream(bis)) { + + ZipEntry zipEntry; + while ((zipEntry = zis.getNextEntry()) != null) { + if (!zipEntry.isDirectory()) { + // 璇诲彇鏂囦欢鍐呭鍒板唴瀛� + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int read; + while ((read = zis.read(buffer)) != -1) { + baos.write(buffer, 0, read); + } + + // 鍒涘缓MultipartFile瀵硅薄 + String fileName = zipEntry.getName(); + byte[] content = baos.toByteArray(); + String contentType = determineContentType(fileName); + + MultipartFile multipartFile = new MockMultipartFile( + fileName, // 鏂囦欢鍚� + fileName, // 鍘熷鏂囦欢鍚� + contentType, // 鍐呭绫诲瀷 + content // 鏂囦欢鍐呭 + ); + + multipartFiles.add(multipartFile); + } + zis.closeEntry(); + } + } + return multipartFiles.toArray(new MultipartFile[0]); + } + + /** + * 鏍规嵁鏂囦欢鍚嶇‘瀹氬唴瀹圭被鍨� + * + * @param fileName 鏂囦欢鍚� + * @return 鍐呭绫诲瀷 + */ + private static String determineContentType(String fileName) { + String extension = ""; + int i = fileName.lastIndexOf('.'); + if (i > 0) { + extension = fileName.substring(i + 1).toLowerCase(); + } + + switch (extension) { + case "txt": + return "text/plain"; + case "html": + case "htm": + return "text/html"; + case "pdf": + return "application/pdf"; + case "jpg": + case "jpeg": + return "image/jpeg"; + case "png": + return "image/png"; + case "gif": + return "image/gif"; + case "doc": + case "docx": + return "application/msword"; + case "xls": + case "xlsx": + return "application/vnd.ms-excel"; + case "xml": + return "application/xml"; + case "json": + return "application/json"; + default: + return "application/octet-stream"; + } + } } \ No newline at end of file -- Gitblit v1.9.3