新增 base64 编码上传附件接口

This commit is contained in:
chenbowen
2025-10-30 11:15:38 +08:00
parent 744567d999
commit b618f833d1
5 changed files with 78 additions and 1 deletions

View File

@@ -12,7 +12,7 @@ public class GatewayWebClientConfiguration {
private final int maxInMemorySize;
public GatewayWebClientConfiguration(
@Value("${databus.gateway.web-client.max-in-memory-size:2097152}") int maxInMemorySize) {
@Value("${databus.gateway.web-client.max-in-memory-size:20971520}") int maxInMemorySize) {
this.maxInMemorySize = maxInMemorySize;
}

View File

@@ -76,6 +76,13 @@ public class FileController {
return success(fileWhitReturn);
}
@PostMapping("/upload-base64-with-return")
@Operation(summary = "上传文件Base64编码携带返回实体", description = "支持通过Base64编码上传文件内容并返回完整文件信息")
public CommonResult<FileRespVO> uploadFileBase64WithReturn(@Valid @RequestBody FileUploadBase64ReqVO uploadReqVO) throws Exception {
FileRespVO fileRespVO = fileService.createFileFromBase64WithReturn(uploadReqVO);
return success(fileRespVO);
}
@GetMapping("/presigned-url")
@Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
@Parameters({

View File

@@ -0,0 +1,28 @@
package com.zt.plat.module.infra.controller.admin.file.vo.file;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
/**
* @author chenbowen
*/
@Schema(description = "管理后台 - 上传文件Base64编码 Request VO")
@Data
public class FileUploadBase64ReqVO {
@Schema(description = "文件内容Base64编码", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "文件内容不能为空")
private String base64Content;
@Schema(description = "文件名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "example.pdf")
@NotBlank(message = "文件名称不能为空")
private String fileName;
@Schema(description = "文件目录", example = "XXX/YYY")
private String directory;
@Schema(description = "是否加密", example = "false")
private Boolean encrypt = false;
}

View File

@@ -5,6 +5,7 @@ import com.zt.plat.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FileRespVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FileUploadBase64ReqVO;
import com.zt.plat.module.infra.dal.dataobject.file.FileDO;
import jakarta.validation.constraints.NotEmpty;
import lombok.SneakyThrows;
@@ -41,6 +42,14 @@ public interface FileService {
@SneakyThrows
FileRespVO createFileWhitReturn(byte[] content, String name, String directory, String type, Boolean encrypt);
/**
* 通过 Base64 编码内容保存文件,并返回完整的文件信息
*
* @param uploadReqVO Base64上传请求VO
* @return 文件信息
*/
FileRespVO createFileFromBase64WithReturn(FileUploadBase64ReqVO uploadReqVO) throws Exception;
/**
* 生成文件预签名地址信息
*

View File

@@ -12,6 +12,7 @@ import com.zt.plat.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FileRespVO;
import com.zt.plat.module.infra.controller.admin.file.vo.file.FileUploadBase64ReqVO;
import com.zt.plat.module.infra.dal.dataobject.file.FileDO;
import com.zt.plat.module.infra.dal.mysql.file.FileMapper;
import com.zt.plat.module.infra.dal.redis.RedisKeyConstants;
@@ -133,6 +134,38 @@ public class FileServiceImpl implements FileService {
return BeanUtils.toBean(entity, FileRespVO.class);
}
@Override
public FileRespVO createFileFromBase64WithReturn(FileUploadBase64ReqVO uploadReqVO) throws Exception {
// 1. 解码 Base64 内容
byte[] content = decodeBase64Content(uploadReqVO.getBase64Content());
// 2. 推断文件类型
String contentType = FileTypeUtils.getMineType(content, uploadReqVO.getFileName());
// 3. 上传文件并返回完整信息
return createFileWhitReturn(content, uploadReqVO.getFileName(), uploadReqVO.getDirectory(),
contentType, uploadReqVO.getEncrypt());
}
/**
* 解码 Base64 内容
*
* @param base64Content Base64 编码的内容
* @return 解码后的字节数组
*/
private byte[] decodeBase64Content(String base64Content) {
try {
// 移除可能存在的 Base64 数据 URL 前缀 (例如: data:image/png;base64,)
String actualBase64 = base64Content;
if (base64Content.contains(",")) {
actualBase64 = base64Content.substring(base64Content.indexOf(",") + 1);
}
return Base64.getDecoder().decode(actualBase64);
} catch (IllegalArgumentException e) {
throw exception(FILE_IS_EMPTY);
}
}
private FileDO uploadFile(byte[] content, String name, String directory, String type, Boolean encrypt) throws Exception {
String aesIvBase64 = null;