zqy
2025-04-15 3ee06bd46c3809fdd00e509debd97dcd039ed031
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
package com.ruoyi.web.controller.zhang;
 
import com.qcloud.cos.utils.IOUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.service.impl.GetOrPut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
 
@RestController
@RequestMapping("/cos")
public class CosController {
 
    @Autowired
    private GetOrPut getOrPut;
 
    // 文件上传
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        String key;
        try {
           key =  getOrPut.uploadFile(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("dfsdgd");
        HashMap<String,String> map = new HashMap<>();
 
        int dotIndex = key.lastIndexOf('/');
        map.put("key", key.substring(dotIndex + 1));
        map.put("path",key);
        return AjaxResult.success(map);
    }
 
    // 文件下载
    @GetMapping("/download")
    public void download(@RequestParam String key, HttpServletResponse response) {
        try (InputStream inputStream = getOrPut.getFileStream(key)) {
 
            String fileExtension = getFileExtension(key);
            String contentType = getContentType(fileExtension);
 
            response.setContentType(contentType);
 
            response.setHeader("Content-Disposition", "attachment; filename=\"" + key + "\"");
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("文件下载失败", e);
        }
    }
 
 
    private String getFileExtension(String key) {
        int dotIndex = key.lastIndexOf('.');
        if (dotIndex == -1) {
            return "";  // 没有扩展名
        }
        return key.substring(dotIndex + 1).toLowerCase();
    }
 
    private String getContentType(String fileExtension) {
        switch (fileExtension) {
            case "jpg":
            case "jpeg":
                return "image/jpeg";
            case "png":
                return "image/png";
            case "gif":
                return "image/gif";
            case "pdf":
                return "application/pdf";
            case "txt":
                return "text/plain";
            default:
                return "application/octet-stream";  // 默认类型
        }
    }
 
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam String key){
        try {
            getOrPut.deleteFile(key);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
 
        return AjaxResult.success("文件删除成功");
    }
}