puzhibing
2026-05-25 31840a27145351390e4a876292b4a96be7908161
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.xiaopeng.operation.controller;
 
 
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;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
/**
 * <p>
 * 任务申诉 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-05-28
 */
@Api(tags = "阿里云文件上传")
@RestController
@RequestMapping("")
public class OssController {
 
    @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();
            //上传文件
            aliOssService.uploadFile(file.getBytes(), fileId);
            String downloadUrl = aliOssService.getDownloadUrl(fileId, fileName,true);
            FileUploadVO fileUploadVO = new FileUploadVO();
            fileUploadVO.setId(fileId);
            fileUploadVO.setName(fileName);
            fileUploadVO.setUrl(downloadUrl);
 
//            FileUploadVO fileUploadVO = new FileUploadVO();
//            fileUploadVO.setId(UUID.randomUUID().toString());
//            fileUploadVO.setName("测试.jpg");
//            fileUploadVO.setUrl("https://huishou-1323682843.cos.ap-nanjing.myqcloud.com/images/552a3a31-e4dd-4a28-8748-fd8b87dd3055.png");
            return R.ok(fileUploadVO);
        } catch (Exception e) {
            System.err.println("上传文件失败"+e);
            return R.fail(e.getMessage());
        }
    }
 
 
 
 
    @PostMapping(value = "/maintenance-app/base/v1/oss/upload",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "文件上传",tags = "文件上传")
    public R<FileUploadVO> uploadOSS1(@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();
            //上传文件
            aliOssService.uploadFile(file.getBytes(), fileId);
            String downloadUrl = aliOssService.getDownloadUrl(fileId, fileName,true);
            FileUploadVO fileUploadVO = new FileUploadVO();
            fileUploadVO.setId(fileId);
            fileUploadVO.setName(fileName);
            fileUploadVO.setUrl(downloadUrl);
 
//            FileUploadVO fileUploadVO = new FileUploadVO();
//            fileUploadVO.setId(UUID.randomUUID().toString());
//            fileUploadVO.setName("测试.jpg");
//            fileUploadVO.setUrl("https://huishou-1323682843.cos.ap-nanjing.myqcloud.com/images/552a3a31-e4dd-4a28-8748-fd8b87dd3055.png");
            return R.ok(fileUploadVO);
        } catch (Exception e) {
            System.err.println("上传文件失败"+e);
            return R.fail(e.getMessage());
        }
    }
 
 
 
    @PostMapping(value = "/maintenance-app/base/v1/oss/getUploadOSSUrl")
    @ApiOperation(value = "获取文件上传地址",tags = "文件上传")
    public R<String> getUploadOSSUrl(@RequestParam("fileId") String fileId) {
        try {
            // 上传并返回访问地址
            String uploadUrl = aliOssService.getUploadUrl(fileId, true);
            return R.ok(uploadUrl);
        } catch (Exception e) {
            System.err.println("上传文件失败"+e);
            return R.fail(e.getMessage());
        }
    }
 
 
 
    @PostMapping(value = "/base/v1/oss/getUploadOSSUrl")
    @ApiOperation(value = "获取文件上传地址",tags = "文件上传")
    public R<String> getUploadOSSUrl1(@RequestParam("fileId") String fileId) {
        try {
            // 上传并返回访问地址
            String uploadUrl = aliOssService.getUploadUrl(fileId, true);
            return R.ok(uploadUrl);
        } catch (Exception e) {
            System.err.println("上传文件失败"+e);
            return R.fail(e.getMessage());
        }
    }
 
}