mitao
2025-01-03 75aa42c51ae2a63d7c1e5e813c0a88fd303bdbf4
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
package com.sinata.web.controller.applet;
 
import com.sinata.common.core.domain.R;
import com.sinata.common.exception.ServiceException;
import com.sinata.system.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.Objects;
 
/**
 * @author mitao
 * @date 2024/12/23
 */
@Api(tags = {"文件上传接口"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/applet/file")
public class AppFileController {
    private final OssService ossService;
 
    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public R<String> upload(@RequestPart("file") MultipartFile file) {
 
        if (Objects.isNull(file)) {
            throw new ServiceException("文件不能为空");
        }
        String fileUrl;
        try {
            fileUrl = ossService.uploadFile(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok(fileUrl);
    }
}