zhanghua
2025-03-04 0f5901bbc027e2e8d934280ca659734a61f67378
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
package com.ycl.controller;
 
import com.ycl.api.CommonResult;
import com.ycl.service.oss.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.UnsupportedEncodingException;
 
@RestController
@Api(tags = "文件上传")
@RequestMapping("/file")
public class FileController {
 
//    private MediaFileUtil mediaFileUtil;
//    @Autowired
//    public void setMediaFileUtil(MediaFileUtil mediaFileUtil) {
//        this.mediaFileUtil = mediaFileUtil;
//    }
 
//    /**
//     * 上传图片
//     * @param file
//     * @return
//     */
//    @ApiOperation("上传图片")
//    @RequestMapping(value = "/medias", method = RequestMethod.POST)
//    public CommonResult<Media> mediaUpload(@RequestParam("file") MultipartFile file) {
//        try {
//            PictureZoomParameter zoomPar = PictureZoomParameter.getBoardPar();
//            Media res = mediaFileUtil.save(file, zoomPar);
//
//            return CommonResult.success(res, "ok");
//        } catch (Exception e) {
//            return CommonResult.failed("图片格式只能为jpg、jpeg、png,只修改文件的后缀无效!");
//        }
//    }
 
//    @ApiOperation("删除图片")
//    @RequestMapping(value = "media/delete", method = RequestMethod.DELETE)
//    public CommonResult<Boolean> delete(@RequestBody Media media) {
//        try {
//            mediaFileUtil.removeMedia(media);
//            return CommonResult.success(true, "ok");
//        } catch (Exception e) {
//            return CommonResult.failed("fail!");
//        }
//    }
 
    @Autowired
    private OssService ossService;
 
    @ApiOperation(value = "上传图片")
    @PostMapping("/medias")
    public CommonResult<String> uploadImages(MultipartFile file) {
        String url = ossService.uploadImages(file);
        // String url = "https://xshcs.com/img/ercode.7a421889.png?v=1";
        return CommonResult.success(url, "ok");
    }
 
    @ApiOperation(value = "删除图片")
    @PostMapping("media/delete")
    public CommonResult<Boolean> deleteImages(String fileUrl) {
        boolean flag = false;
        try {
            flag = ossService.deleteImages(fileUrl);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        if (flag) {
            return CommonResult.success(true, "删除成功");
        }
        return CommonResult.failed("fail!");
    }
 
    @ApiOperation(value = "上传视频")
    @PostMapping("/video")
    public CommonResult<String> uploadVideo(MultipartFile file) {
        String url = ossService.uploadVideo(file);
        return CommonResult.success(url, "ok");
    }
 
 
}