fix(下载): 各模块结果下载由流式代理改 302 重定向到对象存储直链——原实现在 Controller 内边读边写代理整个文件,请求线程被下载时长占满;且恒回 200 全量,客户端 Range 断点续传实际失效

覆盖 brand(/tasks/{taskId}/download)、delete-brand(任务 + 结果两处)、appearance-patent、
patrol-delete、price-track、withdraw、query-asin、product-risk、shop-match、shop-data-crawl
共 10 个 Controller,与既有的 convert/dedupe/split 302 实现对齐(ResponseEntity.status(FOUND)
.location(直链) + Cache-Control no-store),并补重定向 info 日志便于排查。

前端各 tab 本就通过 saveUrlWithProgress(url, filename) 传入文件名与下载 id,不依赖
Content-Disposition,故无需前端改动即可同时获得断点续传与进度回调。
验证:两节点重启后逐个实测 11 个端点均 302(含 brand 任务下载与巡店/跟价的有文件记录);
无文件时保持既有业务错误响应(HTTP 200 + success:false)。
This commit is contained in:
2026-09-11 15:43:46 +08:00
parent 39fb1f1395
commit 3fd28fb3c3
10 changed files with 150 additions and 239 deletions
@@ -1,7 +1,6 @@
package com.nanri.aiimage.modules.appearancepatent.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.util.DownloadHeaderUtil;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParseRequest;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParsedGroupPageDto;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParsedPayloadDto;
@@ -17,7 +16,10 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -28,7 +30,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import com.nanri.aiimage.modules.task.model.dto.TaskProgressLightRequest;
@@ -36,6 +37,7 @@ import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/appearance-patent")
@Tag(name = "外观专利检测", description = "外观专利检测任务接口。前端上传 Excel 后由 Java 解析并创建任务;Python 回传商品数据;Java 负责攒批调用 LLM 检测、补齐子行、生成最终 xlsx 并上传 OSS。")
public class AppearancePatentController {
@@ -156,30 +158,21 @@ public class AppearancePatentController {
@GetMapping("/results/{resultId}/download")
@Operation(summary = "下载外观专利检测结果文件")
public void downloadResult(
public ResponseEntity<Void> downloadResult(
@Parameter(description = "结果记录 ID", required = true, example = "1001")
@PathVariable Long resultId,
@Parameter(description = "当前用户 ID", required = true, example = "1")
@RequestParam("user_id") Long userId,
jakarta.servlet.http.HttpServletResponse response) {
@RequestParam("user_id") Long userId) {
String url = service.resolveResultDownloadUrl(resultId, userId);
String filename = service.resolveResultDownloadFilename(resultId, userId);
if (url == null || url.isBlank()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "暂无可下载结果");
}
try {
response.setContentType("application/octet-stream");
DownloadHeaderUtil.setAttachment(response, filename);
try (InputStream in = URI.create(url).toURL().openStream()) {
byte[] buffer = new byte[65536];
int read;
while ((read = in.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, read);
}
response.getOutputStream().flush();
}
} catch (Exception ex) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "下载失败");
}
// 结果文件常见百 MB 级:改为 302 重定向到对象存储直链,避免 Tomcat 请求线程被整个下载时长占满,
// 并让客户端 Range 断点续传生效。
log.info("[外观专利] 结果文件下载重定向 结果ID={} 用户ID={}", resultId, userId);
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create(url))
.header(HttpHeaders.CACHE_CONTROL, "no-store")
.build();
}
}