From f6d28fec075936b9269d25212d4b0b1c3c4c9f47 Mon Sep 17 00:00:00 2001 From: xupei Date: Tue, 21 Apr 2026 17:21:41 +0800 Subject: [PATCH] =?UTF-8?q?BUGFIX:=E4=B8=BA=E4=B8=8B=E8=BD=BDflux=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=B7=BB=E5=8A=A0=E9=87=8D=E8=AF=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../da/service/impl/GenerateServiceImpl.java | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java b/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java index d23c7b6b..bbf5111d 100644 --- a/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java @@ -3934,11 +3934,48 @@ public class GenerateServiceImpl extends ServiceImpl i } public byte[] downloadVideoOrImage(String url) { - try (CloseableHttpClient client = HttpClients.createDefault(); - InputStream in = client.execute(new HttpGet(url)).getEntity().getContent()) { - return IOUtils.toByteArray(in); - } catch (IOException e) { - throw new RuntimeException(e); + int maxRetries = 3; + int retryDelayMs = 1000; + IOException lastException = null; + + for (int attempt = 1; attempt <= maxRetries; attempt++) { + try { + return downloadWithTimeout(url, 30000, 60000); + } catch (IOException e) { + lastException = e; + log.warn("下载失败 (尝试 {}/{}): {}", attempt, maxRetries, e.getMessage()); + + if (attempt < maxRetries) { + try { + Thread.sleep((long) retryDelayMs * attempt); // 递增延迟 + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new RuntimeException(ie); + } + } + } + } + + throw new RuntimeException("下载失败,已重试 " + maxRetries + " 次", lastException); + } + + private byte[] downloadWithTimeout(String url, int connectTimeout, int socketTimeout) throws IOException { + RequestConfig requestConfig = RequestConfig.custom() + .setConnectTimeout(connectTimeout) + .setSocketTimeout(socketTimeout) + .setConnectionRequestTimeout(connectTimeout) + .build(); + + try (CloseableHttpClient client = HttpClients.custom() + .setDefaultRequestConfig(requestConfig) + .build(); + CloseableHttpResponse response = client.execute(new HttpGet(url))) { + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) { + throw new IOException("HTTP Error: " + statusCode); + } + return IOUtils.toByteArray(response.getEntity().getContent()); } }