田源
2024-08-23 9e20b9fb77a41cb5b4a6eb6213fc51cab1f0bb91
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
package com.vci.web.controller;
 
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.util.ControllerUtil;
import com.vci.starter.web.util.LangBaseUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.web.service.VciFileDownloadServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
 
/**
 * 文件下载控制器
 * @author dangsn
 * @date 2020/08/11
 */
@RestController()
@RequestMapping("/vciFileDownloadController")
public class VciFileDownloadController {
 
    /**
     * 文件下载服务
     */
    @Autowired
    private VciFileDownloadServiceI vciFileDownloadServiceI;
 
    /**
     * 根据文件主键下载文件
     * @param fileOid 文件的主键,不超过get的限制长度
     * @param downloadUUID 下载许可码
     * @param response 响应的对象
     * @return 执行结果,只有下载失败的时候才会这样
     */
    @GetMapping("/downloadByFileOid")
    public void downloadByFileOid(String fileOid, String downloadUUID, HttpServletResponse response){
        String filePath = vciFileDownloadServiceI.downloadFileByOid(fileOid);
        try {
            ControllerUtil.writeFileToResponse(response,filePath);
        } catch (IOException e) {
            String msg = LangBaseUtil.getErrorMsg(e);
            try {
                ControllerUtil.writeDataToResponse(response,StringUtils.isNotBlank(msg)?msg.getBytes():new byte[0],null);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
 
    /**
     * 根据文件主键下载文件
     * @param fileOid 文件的主键,不限制长度
     * @param downloadUUID 下载许可码
     * @param response 响应的对象
     * @return 执行结果,只有下载失败的时候才会这样
     */
    @PostMapping("/downloadByFileOidPost")
    public void downloadByFileOidPost(String fileOid, String downloadUUID, HttpServletResponse response){
        downloadByFileOid(fileOid,downloadUUID,response);
    }
 
    /**
     * 分片下载文件
     * @param fileOid 文件主键
     * @param offSet 偏移量
     * @return 文件内容
     */
    @PostMapping("/sectionDownloadByFileOid")
    public BaseResult sectionDownloadByFileOid(String fileOid, long offSet){
        BaseResult<byte[]> baseResult = vciFileDownloadServiceI.sectionDownloadByFileOid(fileOid, offSet);
        return baseResult;
    }
 
    /**
     * 文件删除
     * @param fileOids 文件主键
     * @return 文件内容
     */
    @DeleteMapping("/deleteFile")
    public BaseResult deleteFile(String fileOids){
        List<String> fileOidCollection = VciBaseUtil.str2List(fileOids);
        return vciFileDownloadServiceI.deleteFile(fileOidCollection);
    }
 
}