From 2bf7153f5d4bacf652308b221b1651dd496f4dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Tue, 2 Jun 2026 17:54:31 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E5=A4=A7=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E7=89=87=E6=96=AD=E7=82=B9=E7=BB=AD=E4=BC=A0?= =?UTF-8?q?=20=E6=97=A0=E6=B3=95=E7=BB=AD=E4=BC=A0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oss/client/AbstractOssClientImpl.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java index da94ca63e..0fc385069 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java @@ -11,7 +11,6 @@ import org.dromara.common.oss.model.GetObjectResult; import org.dromara.common.oss.model.HandleAsyncResult; import org.dromara.common.oss.model.Options; import org.dromara.common.oss.model.PutObjectResult; -import org.jspecify.annotations.NullMarked; import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.core.async.AsyncResponseTransformer; @@ -426,9 +425,25 @@ public abstract class AbstractOssClientImpl implements OssClient { */ @Override public PutObjectResult bucketUpload(String bucket, String key, InputStream in, long contentLength, Options options) { - options.setLength(contentLength); - AsyncRequestBody body = AsyncRequestBody.fromInputStream(in, contentLength, asyncExecutor); - return bucketUpload(bucket, key, body, options); + Path tempFile = null; + try { + tempFile = Files.createTempFile("ruoyi-oss-upload-", ".tmp"); + try (OutputStream out = Files.newOutputStream(tempFile)) { + in.transferTo(out); + } + options.setLength(Files.size(tempFile)); + return bucketUpload(bucket, key, tempFile, options); + } catch (Exception e) { + throw toStorageException(e); + } finally { + if (tempFile != null) { + try { + Files.deleteIfExists(tempFile); + } catch (IOException ignored) { + // 临时文件清理失败不影响上传结果。 + } + } + } } /** @@ -456,11 +471,9 @@ public abstract class AbstractOssClientImpl implements OssClient { */ @Override public PutObjectResult bucketUpload(String bucket, String key, byte[] data, Options options) { - try (ByteArrayInputStream in = new ByteArrayInputStream(data)) { - return bucketUpload(bucket, key, in, data.length, options); - } catch (Exception e) { - throw toStorageException(e); - } + options.setLength((long) data.length); + AsyncRequestBody body = AsyncRequestBody.fromBytes(data); + return bucketUpload(bucket, key, body, options); } /** @@ -485,7 +498,6 @@ public abstract class AbstractOssClientImpl implements OssClient { * @param options 上传选项 * @return 上传结果 */ - @NullMarked private PutObjectResult bucketUpload(String bucket, String key, AsyncRequestBody body, Options options) { // 优先使用body中的内容大小,如果不存在,再获取可选项中的 Long contentLength = body.contentLength().orElse(options.getLength()); @@ -503,7 +515,7 @@ public abstract class AbstractOssClientImpl implements OssClient { .metadata(metadata); }, transferListeners); if (result.isFailure()) { - throw S3StorageException.form(result.error()); + throw toStorageException(result.error()); } Optional opt = result.getResult(); if (opt.isEmpty()) {