From f638f426f6a424d5f0847b7c44a3459ae9c2fa43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E7=BB=B4=E7=82=B3?= Date: Fri, 25 Apr 2025 15:10:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=96=87=E4=BB=B6=EF=BC=8C=E8=A7=A3=E5=8E=8Bzip?= =?UTF-8?q?=EF=BC=8C=E8=8E=B7=E5=8F=96=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/Gcc/GccController.java | 4 + .../controller/admin/GccController.java | 4 - .../admin/autoTools/AutoToolsController.java | 4 + .../vo/StudentUploadFileZipReqVo.java | 19 ++++ .../service/auto_tools/AutoToolsService.java | 26 +++++ .../auto_tools/AutoToolsServiceImpl.java | 97 +++++++++++++++++++ 6 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Gcc/GccController.java delete mode 100644 exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/GccController.java create mode 100644 exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java create mode 100644 exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/vo/StudentUploadFileZipReqVo.java create mode 100644 exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsService.java create mode 100644 exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsServiceImpl.java diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Gcc/GccController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Gcc/GccController.java new file mode 100644 index 00000000..9c00bb96 --- /dev/null +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Gcc/GccController.java @@ -0,0 +1,4 @@ +package pc.exam.pp.module.judgement.controller.admin.Gcc; + +public class GccController { +} diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/GccController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/GccController.java deleted file mode 100644 index 7076e9dc..00000000 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/GccController.java +++ /dev/null @@ -1,4 +0,0 @@ -package pc.exam.pp.module.judgement.controller.admin; - -public class GccController { -} diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java new file mode 100644 index 00000000..669cce78 --- /dev/null +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java @@ -0,0 +1,4 @@ +package pc.exam.pp.module.judgement.controller.admin.autoTools; + +public class AutoToolsController { +} diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/vo/StudentUploadFileZipReqVo.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/vo/StudentUploadFileZipReqVo.java new file mode 100644 index 00000000..9f789cf4 --- /dev/null +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/vo/StudentUploadFileZipReqVo.java @@ -0,0 +1,19 @@ +package pc.exam.pp.module.judgement.controller.admin.autoTools.vo; + + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class StudentUploadFileZipReqVo { + + @Schema(description = "学号") + private Long studentId; + + @Schema(description = "试卷号") + private String paperId; + + @Schema(description = "上传文件路径") + private String filePath; + +} diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsService.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsService.java new file mode 100644 index 00000000..5cd1cefa --- /dev/null +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsService.java @@ -0,0 +1,26 @@ +package pc.exam.pp.module.judgement.service.auto_tools; + +public interface AutoToolsService { + + + // 文件自动上传 + + // Boolean autoFileTools(Long studentId, String paperId); + + /** + * 文件下载 + * @param fileUrl 文件url + * @param filePath 要下载的文件路径 + * @return 下载后的文件path + */ + String downloadStudentFile(String fileUrl, String filePath); + + // + + /** + * 解压文件 + * @param zipFilePath zip文件路径 + * @return 解压后的目录 + */ + String unzipToNamedFolder(String zipFilePath); +} diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsServiceImpl.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsServiceImpl.java new file mode 100644 index 00000000..1021ed73 --- /dev/null +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/auto_tools/AutoToolsServiceImpl.java @@ -0,0 +1,97 @@ +package pc.exam.pp.module.judgement.service.auto_tools; + +import org.springframework.stereotype.Service; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@Service +public class AutoToolsServiceImpl implements AutoToolsService{ + + @Override + public String downloadStudentFile(String fileUrl, String filePath) { + try { + URL url = new URL(fileUrl); + URLConnection connection = url.openConnection(); + + String fileName = new File(url.getPath()).getName(); + File dir = new File(filePath); + if (!dir.exists()) dir.mkdirs(); + + File saveFile = new File(dir, fileName); + + try (InputStream in = connection.getInputStream(); + FileOutputStream out = new FileOutputStream(saveFile)) { + + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + + System.out.println("✅ 下载成功: " + saveFile.getAbsolutePath()); + return saveFile.getAbsolutePath(); + } + } catch (IOException e) { + System.err.println("❌ 下载失败: " + e.getMessage()); + return null; + } + } + + @Override + public String unzipToNamedFolder(String zipFilePath) { + File zipFile = new File(zipFilePath); + + if (!zipFile.exists() || !zipFile.getName().toLowerCase().endsWith(".zip")) { + System.err.println("❌ 无效 zip 文件: " + zipFilePath); + return null; + } + + // 提取 zip 文件名(无扩展) + String fileNameNoExt = zipFile.getName().replaceAll("(?i)\\.zip$", ""); + // 自动拼接系统适配路径 + File extractDir = new File(zipFile.getParentFile(), fileNameNoExt); + + if (!extractDir.exists()) { + extractDir.mkdirs(); + } + + try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) { + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + File outFile = new File(extractDir, entry.getName()); + + // 防止 Zip 路径穿越攻击 + if (!outFile.getCanonicalPath().startsWith(extractDir.getCanonicalPath())) { + throw new IOException("非法路径: " + entry.getName()); + } + + if (entry.isDirectory()) { + outFile.mkdirs(); + } else { + File parent = outFile.getParentFile(); + if (!parent.exists()) parent.mkdirs(); + + try (FileOutputStream fos = new FileOutputStream(outFile)) { + byte[] buffer = new byte[4096]; + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + } + } + zis.closeEntry(); + } + System.out.println("✅ 解压完成,目录:" + extractDir.getAbsolutePath()); + return extractDir.getAbsolutePath(); + + } catch (IOException e) { + System.err.println("❌ 解压出错: " + e.getMessage()); + return null; + } + } +}