WangHan
2025-04-03 a1b85ef72062ca80db35546e4216dd564f3e0f57
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
package com.iplatform.file.controller;
 
import com.iplatform.base.SystemController;
import com.iplatform.base.config.ApiProperties;
import com.walker.file.FileInfo;
import com.walker.infrastructure.utils.FileCopyUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import com.walker.web.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
@RestController
@RequestMapping("/oss")
public class OssFileApi extends SystemController {
 
    private ApiProperties apiProperties;
 
    @Autowired
    public OssFileApi(ApiProperties apiProperties){
        this.apiProperties = apiProperties;
    }
 
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public void downloadOssFile(@PathVariable(name = "id") String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException("文件参数为空,无法下载文件");
        }
        FileInfo fileInfo = this.acquireFileOperateSpi().getFileInfo(Long.parseLong(id));
        if(fileInfo == null){
            throw new IllegalArgumentException("文件不存在,id=" + id);
        }
 
        // 2024-03-08 为压测提供支持,
        try {
            byte[] fileContent = null;
            if(this.apiProperties.isTimeEnabled()){
                // 压测模式,记录接口调用时间
                fileContent = this.acquirePressureTestFile(fileInfo);
            } else {
                // 正常模式
                fileContent = this.getOssFileData(id);
            }
            this.downloadSimpleFile(fileContent, fileInfo.getFileName());
 
        } catch (IOException e) {
            logger.error("下载oss错误:" + e.getMessage() + ", id=" + id, e);
            ServletUtils.renderString(getResponse(), "下载oss错误:" + e.getMessage() + ", id=" + id);
        }
    }
 
    /**
     * 压测时尝试从缓存查找文件,如果未缓存会把默认64个文件缓存。如果超过64个,先清除缓存,并重新开始缓存后续64个,循环往复。
     * @param fileInfo
     * @return
     * @author 时克英
     * @date 2024-03-08
     */
    private byte[] acquirePressureTestFile(FileInfo fileInfo){
        byte[] fileData = null;
        long fileSize = fileInfo.getFileSize();
        if(fileSize <= maxCacheFileSize){
            // 当缓存数量达到上限时,清空重新开始缓存新的
            if(fileContentCache.size() >= maxCacheFileCount){
                fileContentCache.clear();
            }
            // 当文件小于等于1M,并且缓存数量未达到上限时,缓存
            fileData = this.fileContentCache.get(fileInfo.getId());
            if(fileData == null){
                fileData = this.getOssFileData(fileInfo.getId());
                this.fileContentCache.put(fileInfo.getId(), fileData);
                logger.debug("缓存一个oss文件:{},大小:{}", fileInfo.getFileName(), fileSize);
            } else {
                logger.debug("从缓存加载了oss文件:{}", fileInfo.getId());
            }
        } else {
            fileData = this.getOssFileData(fileInfo.getId());
        }
        return fileData;
    }
 
    private Map<String, byte[]> fileContentCache = new ConcurrentHashMap<>(128);
 
    private int maxCacheFileCount = 64;
    private long maxCacheFileSize = 1024 * 1024;
 
    /**
     * 模拟测试。
     */
    @RequestMapping(value = "/test/demo", method = RequestMethod.GET)
    public void downloadOssDemo(){
        String file = "F:/app_ocr_demo/ocr_e_005.jpg";
        try {
            this.downloadSimpleFile(FileCopyUtils.copyToByteArray(new File(file)), "测试图片");
        } catch (IOException e) {
            logger.error("下载oss demo错误:" + e.getMessage() + ", id=" + file, e);
            ServletUtils.renderString(getResponse(), "下载oss demo错误:" + e.getMessage() + ", id=" + file);
        }
    }
 
    @RequestMapping(value = "/test/upload", method = RequestMethod.GET)
    public ResponseValue demoUploadOss() throws Exception{
        File file = new File("F:/app_ocr_demo/ocr_e_005.jpg");
        FileInputStream inputStream = new FileInputStream(file);
        FileInfo fileInfo = this.uploadFileToRemote(inputStream, "demo.jpg", null, file.length(), 0, "-1");
        logger.info("fileInfo = {}", fileInfo);
        return ResponseValue.success("上传文件成功," + fileInfo.getUrl());
    }
}