From f0b83406bea6cbc8da30ade1ef4d6ad0e388f69b Mon Sep 17 00:00:00 2001 From: PIG AI Date: Sat, 5 Jul 2025 15:16:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(upms):=20=E6=96=B0=E5=A2=9EByteArrayMultip?= =?UTF-8?q?artFile=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=AE=9E=E7=8E=B0=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=9E=8B=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现基于byte[]的MultipartFile接口适配RemoteFileService - 提供两种构造方式支持直接byte[]或InputStream输入 - 包含完整文件属性(name/originalFilename/contentType)处理方法 - 内置空文件检查和文件大小计算能力 --- .../api/utils/ByteArrayMultipartFile.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/utils/ByteArrayMultipartFile.java diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/utils/ByteArrayMultipartFile.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/utils/ByteArrayMultipartFile.java new file mode 100644 index 000000000..5a73ea1e3 --- /dev/null +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/utils/ByteArrayMultipartFile.java @@ -0,0 +1,124 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.pig4cloud.pigx.admin.api.utils; + +import lombok.Value; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; +import org.springframework.util.FileCopyUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +/** + * 基于字节数组实现的内存型MultipartFile ,适配于 RemoteFileService 的 Upload 方法。 + * + * @author lengleng + * @date 2025/07/05 + */ +@Value +public class ByteArrayMultipartFile implements MultipartFile { + + private final String name; + + private final String originalFilename; + + @Nullable + private final String contentType; + + private final byte[] content; + + /** + * Create a new ByteArrayMultipartFile with the given content. + * + * @param name the name of the file + * @param originalFilename the original filename (as on the client's machine) + * @param contentType the content type (if known) + * @param content the content of the file + */ + public ByteArrayMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, + @Nullable byte[] content) { + + Assert.hasLength(name, "Name must not be empty"); + this.name = name; + this.originalFilename = (originalFilename != null ? originalFilename : ""); + this.contentType = contentType; + this.content = (content != null ? content : new byte[0]); + } + + /** + * Create a new ByteArrayMultipartFile with the given content. + * + * @param name the name of the file + * @param originalFilename the original filename (as on the client's machine) + * @param contentType the content type (if known) + * @param contentStream the content of the file as stream + * @throws IOException if reading from the stream failed + */ + public ByteArrayMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, + InputStream contentStream) throws IOException { + + this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); + } + + @Override + public String getName() { + return this.name; + } + + @Override + @NonNull + public String getOriginalFilename() { + return this.originalFilename; + } + + @Override + @Nullable + public String getContentType() { + return this.contentType; + } + + @Override + public boolean isEmpty() { + return (this.content.length == 0); + } + + @Override + public long getSize() { + return this.content.length; + } + + @Override + public byte[] getBytes() throws IOException { + return this.content; + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(this.content); + } + + @Override + public void transferTo(File dest) throws IOException, IllegalStateException { + FileCopyUtils.copy(this.content, dest); + } + +}