feat(llm): 添加数据集示例文件下载功能

- 新增 downloadExampleExcelFile 方法,用于下载示例 Excel 文件
- 修复了文件未找到时的异常处理
- 优化了文件下载的 HTTP 响应头设置
This commit is contained in:
Liuyang 2025-03-24 15:33:21 +08:00
parent cbb97fc055
commit 3ef7b8e8c7

View File

@ -104,29 +104,30 @@ public class DatasetController {
BeanUtils.toBean(list, DatasetRespVO.class));
}
@GetMapping("/download-example-excel")
@Operation(summary = "下载示例文件")
public ResponseEntity<InputStreamResource> downloadExampleExcelFile (@RequestParam("type") int type, HttpServletResponse response) throws IOException {
FileInfoVO fileInfo = getFileInfo(type);
// 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));
// }
// 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));
}
@GetMapping("/download-example")
@Operation(summary = "下载示例文件")
public CommonResult<String> downloadExampleFile(@RequestParam("type") int type) throws IOException {
public CommonResult<String> downloadExampleFile (@RequestParam("type") int type) throws IOException {
FileInfoVO fileInfo = getFileInfo(type);
ClassPathResource resource = new ClassPathResource("file/dataset_example/" + fileInfo.getFileName());
if (!resource.exists()) {
@ -144,6 +145,7 @@ public class DatasetController {
return CommonResult.success(base64);
}
}
private FileInfoVO getFileInfo (int type) {
switch (type) {
case 1: