From abb6047277d7caf0314ffef7049edc5b0b2d0b1e Mon Sep 17 00:00:00 2001 From: mediabot-pt <295750538+mediabot-pt@users.noreply.github.com> Date: Tue, 30 Jun 2026 18:03:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Qiniu=20=E4=B8=8B=E8=BD=BD=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E6=A0=87=E5=87=86=20HttpClient=EF=BC=8C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA=E5=AE=8C=E6=95=B4=E7=AD=BE=E5=90=8D?= =?UTF-8?q?URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../par/core/storage/QiniuStorageService.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/par-core/src/main/java/com/par/core/storage/QiniuStorageService.java b/par-core/src/main/java/com/par/core/storage/QiniuStorageService.java index 9903a09..0e2fa36 100644 --- a/par-core/src/main/java/com/par/core/storage/QiniuStorageService.java +++ b/par-core/src/main/java/com/par/core/storage/QiniuStorageService.java @@ -1,8 +1,6 @@ package com.par.core.storage; import com.qiniu.common.QiniuException; -import com.qiniu.http.Client; -import com.qiniu.http.Response; import com.qiniu.storage.BucketManager; import com.qiniu.storage.Configuration; import com.qiniu.storage.DownloadUrl; @@ -12,6 +10,12 @@ import com.qiniu.util.Auth; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; + /** * 七牛云存储。 * 上传使用 UploadManager,下载使用 DownloadUrl + Auth 签名。 @@ -22,7 +26,7 @@ public class QiniuStorageService implements StorageService { private UploadManager uploadManager; private BucketManager bucketManager; - private Client qiniuClient; + private HttpClient httpClient; private Auth auth; private String bucket; private String downloadDomain; @@ -57,7 +61,10 @@ public class QiniuStorageService implements StorageService { Configuration cfg = new Configuration(region); this.uploadManager = new UploadManager(cfg); this.bucketManager = new BucketManager(auth, cfg); - this.qiniuClient = new Client(cfg); + this.httpClient = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(5)) + .followRedirects(HttpClient.Redirect.NORMAL) + .build(); log.info("Qiniu 存储就绪: bucket={}, domain={}", bucket, downloadDomain); } @@ -86,16 +93,18 @@ public class QiniuStorageService implements StorageService { log.warn("Qiniu stat 失败 {}: {}", path, e.response != null ? e.response.error : e.getMessage()); } - // 用 DownloadUrl 生成带签名的下载链接,SDK Client 下载 + // 用 DownloadUrl 生成带签名的下载链接 long deadline = System.currentTimeMillis() / 1000 + 3600; try { DownloadUrl url = new DownloadUrl(downloadDomain, useHttps, path); String signedUrl = url.buildURL(auth, deadline); - Response resp = qiniuClient.get(signedUrl); - if (resp.isOK()) return resp.bodyString(); - log.warn("Qiniu 下载失败 {}: HTTP {}", path, resp.statusCode); - } catch (QiniuException e) { - log.warn("Qiniu 下载失败 {}: {}", path, e.response != null ? e.response.error : e.getMessage()); + var req = HttpRequest.newBuilder().uri(URI.create(signedUrl)) + .timeout(Duration.ofSeconds(10)).GET().build(); + var resp = httpClient.send(req, HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() == 200) return resp.body(); + log.warn("Qiniu 下载失败 {}: HTTP {} url={}", path, resp.statusCode(), signedUrl); + } catch (Exception e) { + log.warn("Qiniu 下载失败 {}: {}", path, e.getMessage()); } return null; }