whyclxw
2024-08-28 0ae20eb0d70fb49c695716aaca7ea4634ac8d8fc
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
package com.whyc.controller;
 
import com.whyc.entity.MinIoUploadVo;
import com.whyc.entity.R;
import com.whyc.service.MinioService;
import io.minio.messages.DeleteObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * @author 陳樂
 * @version 1.0.0
 * @ClassName UploadController.java
 * @Description 文件上传处理
 * @createTime 2022年03月12日 14:03:00
 */
@RestController
@Slf4j
@RequestMapping("test")
@Api(tags = "测试")
public class UploadController {
 
    @Autowired
    private MinioService minioService;
 
    /**
     * 文件下载
     *
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/upload")
    @ApiOperation("上传")
    public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //String strDir = request.getParameter("bucketName") == null ? "car" : request.getParameter("bucketName");
        String strDir ="test";
        try {
            MinIoUploadVo uploadVo = minioService.upload(strDir, file);
            return R.ok().message("文件上传成功").data(uploadVo);
        } catch (Exception e) {
            log.error("上传文件失败,msg={}", e.getMessage());
            e.printStackTrace();
            return R.error();
        }
    }
 
 
 
    /**
     * 文件下载公共组件
     *
     * @param minIoUploadVo
     * @param response
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(MinIoUploadVo minIoUploadVo, HttpServletResponse response) {
 
        try {
            minioService.download(response, minIoUploadVo.getBucketName(), minIoUploadVo.getMinFileName());
        } catch (Exception e) {
            log.error("下载文件失败,msg={}", e.getMessage());
            e.printStackTrace();
        }
    }
 
    /**
     * 文件服务器文件删除(支持多个)
     *
     * @param minIoUploadVo
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public R delete(MinIoUploadVo minIoUploadVo) {
 
        try {
            List<DeleteObject> objectList = new ArrayList<>();
 
            String fileDir = minIoUploadVo.getBucketName().concat("/");
            Arrays.stream(minIoUploadVo.getMinFileName().split(",")).forEach(name -> objectList.add(new DeleteObject(fileDir.concat(name))));
 
            minioService.removeObject("car", objectList);
 
            return R.ok().message("删除成功");
        } catch (Exception e) {
            log.error("删除文件失败,msg={}", e.getMessage());
            e.printStackTrace();
            return R.error();
        }
    }
 
}