refactor(llm): 重构数据集示例文件下载接口

- 将下载示例文件接口的返回类型从 ResponseEntity<InputStreamResource> 改为 CommonResult<String>
- 使用 Base64 编码文件内容后返回,以适应前端需求
- 保留原始逻辑的注释代码,便于未来参考
This commit is contained in:
Liuyang 2025-03-24 13:45:36 +08:00
parent 479c4f56a2
commit 44609a674d

View File

@ -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<InputStreamResource> downloadExampleFile (@RequestParam("type") int type, HttpServletResponse response) throws IOException {
public CommonResult<String> 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<InputStreamResource> 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) {