refactor(llm): 重构数据集示例文件下载功能
- 修改下载示例文件接口路径,将 "/download-example-excel"改为 "/download-example" - 优化下载逻辑,使用 ResponseEntity<InputStreamResource> 作为返回类型 - 删除冗余的 Base64 编码下载方法
This commit is contained in:
parent
3ef7b8e8c7
commit
224ff36343
@ -104,9 +104,9 @@ public class DatasetController {
|
||||
BeanUtils.toBean(list, DatasetRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/download-example-excel")
|
||||
@GetMapping("/download-example")
|
||||
@Operation(summary = "下载示例文件")
|
||||
public ResponseEntity<InputStreamResource> downloadExampleExcelFile (@RequestParam("type") int type, HttpServletResponse response) throws IOException {
|
||||
public ResponseEntity<InputStreamResource> downloadExampleFile (@RequestParam("type") int type, HttpServletResponse response) throws IOException {
|
||||
FileInfoVO fileInfo = getFileInfo(type);
|
||||
|
||||
// 从 resources/file/dataset_example 目录加载文件
|
||||
@ -125,26 +125,26 @@ public class DatasetController {
|
||||
.body(new InputStreamResource(inputStream));
|
||||
}
|
||||
|
||||
@GetMapping("/download-example")
|
||||
@Operation(summary = "下载示例文件")
|
||||
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()) {
|
||||
throw new FileNotFoundException("文件未找到: " + fileInfo.getFileName());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
// @GetMapping("/download-example")
|
||||
// @Operation(summary = "下载示例文件")
|
||||
// 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()) {
|
||||
// throw new FileNotFoundException("文件未找到: " + fileInfo.getFileName());
|
||||
// }
|
||||
//
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
private FileInfoVO getFileInfo (int type) {
|
||||
switch (type) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user