feat: 资源列表批量下载优化

This commit is contained in:
2025-11-06 17:05:33 +08:00
parent 33177a56ac
commit cb66f9f7e1
15 changed files with 365 additions and 188 deletions
@@ -26,12 +26,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
@@ -601,26 +597,20 @@ public class OrderFileController {
public ResponseEntity<StreamingResponseBody> downloadFile(@RequestBody FileDownloadRequest request) throws IOException {
String fileUrl = request.getUrl();
log.info("📥 开始代理下载文件: {}", fileUrl);
if (fileUrl == null || fileUrl.trim().isEmpty()) {
throw new IllegalArgumentException("文件URL不能为空");
}
// 从外部 CDN 下载文件
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
// 设置超时和默认User-Agent
connection.setConnectTimeout(10000); // 10秒连接超时
connection.setReadTimeout(300000); // 5分钟读取超时,避免网络差时频繁超时
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
// 获取文件大小
long fileSize = connection.getContentLengthLong();
String fileName = extractFileNameFromUrl(fileUrl);
log.info("📊 文件信息: 名称={}, 大小={}MB", fileName, fileSize / (1024 * 1024));
// 构建响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
@@ -656,7 +646,6 @@ public class OrderFileController {
throw e;
}
};
return ResponseEntity.ok()
.headers(headers)
.body(streamingBody);
@@ -680,7 +669,6 @@ public class OrderFileController {
if (orderNo == null || orderNo.trim().isEmpty()) {
throw new IllegalArgumentException("订单编号不能为空");
}
try {
// 查询订单所有文件
List<OrderFile> orderFiles = orderFileService.getOrderFileByOrderNo(orderNo);
@@ -817,4 +805,16 @@ public class OrderFileController {
}
}
@Operation(summary = "根据文件id获取下载链接")
@GetMapping("/file/download/{fileId}")
public String getFileDownLoadUrl(@PathVariable("fileId") String fileId) {
if(fileId==null && fileId==""){
return "";
}
String url= fileCenterService.getFileDownLoadUrl(fileId);
log.info("返回的下载url为{}",url);
return url;
}
}
@@ -70,4 +70,8 @@ public interface FileCenterService {
Result<Boolean> batchPrefetchFiles(@RequestBody List<String> urls,
@RequestParam(value = "batchSize", defaultValue = "100") int batchSize,
@RequestParam(value = "area", required = false) String area);
@GetMapping("/file/download/{id}")
String getFileDownLoadUrl(@PathVariable("id") String id);
}
@@ -73,6 +73,11 @@ public class FileCenterServiceFallbackFactory implements FallbackFactory<FileCen
return Result.failed("CDN批量预热文件失败: " + cause.getMessage());
}
@Override
public String getFileDownLoadUrl(String id) {
return "";
}
};
}
}