liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
xp-robotaxi-operation-boot/src/main/java/com/xiaopeng/operation/controller/OssController.java
@@ -3,6 +3,7 @@
import cn.hutool.core.lang.UUID;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.evi.oss.service.AliOssService;
import com.xiaopeng.operation.vo.FileUploadVO;
import io.swagger.annotations.Api;
@@ -13,6 +14,9 @@
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
 * <p>
@@ -30,12 +34,37 @@
    @Resource
    private AliOssService aliOssService;
    private static final Set<String> ALLOWED_IMAGE_TYPES = new HashSet<>(Arrays.asList(
            ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg"
    ));
    /**
     * 校验文件名安全性和图片类型
     */
    private void validateFileName(String fileName) throws IOException {
        if (fileName == null || fileName.trim().isEmpty()) {
            throw new IOException("文件名不能为空");
        }
        if (fileName.contains("../") || fileName.contains("..\\")) {
            throw new IOException("文件名包含非法字符");
        }
        String fileExtension = "";
        int lastDotIndex = fileName.lastIndexOf(".");
        if (lastDotIndex > 0) {
            fileExtension = fileName.substring(lastDotIndex).toLowerCase();
        }
        if (!ALLOWED_IMAGE_TYPES.contains(fileExtension)) {
            throw new ServiceException("只允许上传图片文件,支持的类型:jpg, jpeg, png, gif, bmp, webp, svg");
        }
    }
    @PostMapping(value = "/base/v1/oss/upload",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "文件上传",tags = "文件上传")
    public R<FileUploadVO> uploadOSS(@RequestParam("file") MultipartFile file) {
        try {
            // 上传并返回访问地址
            String fileName = file.getOriginalFilename();
            validateFileName(fileName);
            String prefix = fileName.substring(fileName.lastIndexOf("."));
            long fileSize = file.getSize();
            String fileId = UUID.randomUUID().toString();
@@ -67,6 +96,7 @@
        try {
            // 上传并返回访问地址
            String fileName = file.getOriginalFilename();
            validateFileName(fileName);
            String prefix = fileName.substring(fileName.lastIndexOf("."));
            long fileSize = file.getSize();
            String fileId = UUID.randomUUID().toString();