From 44609a674d8fc088072ca958d49903de90fff59f Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Mon, 24 Mar 2025 13:45:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(llm):=20=E9=87=8D=E6=9E=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E7=A4=BA=E4=BE=8B=E6=96=87=E4=BB=B6=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将下载示例文件接口的返回类型从 ResponseEntity 改为 CommonResult - 使用 Base64 编码文件内容后返回,以适应前端需求 - 保留原始逻辑的注释代码,便于未来参考 --- .../admin/dataset/DatasetController.java | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/dataset/DatasetController.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/dataset/DatasetController.java index cd4dc1bf4..3c8e685ea 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/dataset/DatasetController.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/dataset/DatasetController.java @@ -23,10 +23,12 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; +import java.util.Base64; import java.util.List; import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; @@ -104,24 +106,43 @@ public class DatasetController { @GetMapping("/download-example") @Operation(summary = "下载示例文件") - public ResponseEntity downloadExampleFile (@RequestParam("type") int type, HttpServletResponse response) throws IOException { + public CommonResult downloadExampleFile(@RequestParam("type") int type) throws IOException { FileInfoVO fileInfo = getFileInfo(type); - - // 从 resources/file/dataset_example 目录加载文件 ClassPathResource resource = new ClassPathResource("file/dataset_example/" + fileInfo.getFileName()); if (!resource.exists()) { throw new FileNotFoundException("文件未找到: " + fileInfo.getFileName()); } - InputStream inputStream = resource.getInputStream(); - HttpHeaders headers = new HttpHeaders(); - headers.add("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileInfo.getFileName(), "UTF-8") + "\""); - - return ResponseEntity.ok() - .headers(headers) - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .body(new InputStreamResource(inputStream)); + try (InputStream inputStream = resource.getInputStream(); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + byteStream.write(buffer, 0, bytesRead); + } + String base64 = Base64.getEncoder().encodeToString(byteStream.toByteArray()); + return CommonResult.success(base64); + } } + // public ResponseEntity downloadExampleFile (@RequestParam("type") int type, HttpServletResponse response) throws IOException { + // FileInfoVO fileInfo = getFileInfo(type); + // + // // 从 resources/file/dataset_example 目录加载文件 + // ClassPathResource resource = new ClassPathResource("file/dataset_example/" + fileInfo.getFileName()); + // if (!resource.exists()) { + // throw new FileNotFoundException("文件未找到: " + fileInfo.getFileName()); + // } + // + // InputStream inputStream = resource.getInputStream(); + // HttpHeaders headers = new HttpHeaders(); + // headers.add("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileInfo.getFileName(), "UTF-8") + "\""); + // + // return ResponseEntity.ok() + // .headers(headers) + // .contentType(MediaType.APPLICATION_OCTET_STREAM) + // .body(new InputStreamResource(inputStream)); + // } + private FileInfoVO getFileInfo (int type) { switch (type) {