13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.ruoyi.web.controller.common;
 
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.manage.domain.SysFileManage;
import com.ruoyi.manage.domain.vo.SysFileManageVO;
import com.ruoyi.manage.service.SysFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 文件管理表 前端控制器
 * </p>
 *
 * @author sunjiawei
 * @since 2025-05-21
 */
@RestController
@RequestMapping("/dp/sysFile")
@Tag(name = "大屏--文件管理")
public class SysFileController {
    @Autowired
    SysFileService sysFileService;
    @Autowired
    RuoYiConfig ruoYiConfig;
 
    /**
     * 查询文件管理列表
     */
    @GetMapping("/listAll")
    @Operation(summary = "查询文件管理列表")
    public AjaxResult listAll()
    {
        return AjaxResult.success(sysFileService.listAll());
    }
 
    /**
     * 单个上传文件
     */
    @PostMapping("/uploadFile")
    @Operation(summary = "单个上传文件")
    public AjaxResult uploadFile(MultipartFile file,String name,String type,
                                 @RequestParam(value = "remark",required = false) String remark) {
        SysFileManage sysFileManage = new SysFileManage();
        sysFileManage.setName(name);
        sysFileManage.setType(type);
        sysFileManage.setRemark(remark);
        try {
            int res = sysFileService.saveFile(file,sysFileManage);
            return AjaxResult.success(res);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
 
    /**
     * 批量删除文件
     */
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除文件")
    public AjaxResult deleteBatch(@RequestBody List<String> ids)
    {
        List<String> res = sysFileService.deleteBatch(ids);
        if(res.size()==0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.success(res);
    }
 
    @PostMapping("/download")
    @Operation(summary = "下载文件")
    public ResponseEntity<FileSystemResource> resourceDownload(String id){
        try {
            SysFileManage sysFileManage = sysFileService.getById(id);
            String filePath = sysFileManage.getFilePath();
            String name = sysFileManage.getName();
            String realFilePath = filePath.replaceFirst("/profile",RuoYiConfig.getProfile());
            String ext = sysFileManage.getExt();
            if(ext!=null && !ext.isEmpty()){
                if(ext.equals("json")){
                    realFilePath = filePath.replaceFirst("/profile",ruoYiConfig.getJson());
                }
            }
            String realFileName = name + realFilePath.substring(realFilePath.indexOf("_") + 1);
 
            File file = new File(realFilePath);
            if(!file.exists()){
                return ResponseEntity.notFound().build();
            }
            FileSystemResource resource = new FileSystemResource(file);
 
            return ResponseEntity.ok()
                    .header("Content-Disposition","attachment;filename=" + realFileName)
                    .body(resource);
 
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }
 
    @GetMapping("/getCount")
    @Operation(summary = "查询总条数")
    public AjaxResult getCount(){
        return AjaxResult.success(sysFileService.queryCount());
    }
 
 
    /**
     * 分页查询文件管理列表
     */
    @PostMapping("/getPageList")
    @Operation(summary = "分页查询文件管理列表")
    public TableDataInfo getPageList(@Validated @RequestBody SysFileManageVO vo) {
        List<SysFileManage> list = sysFileService.queryData(vo);
        return paginate(list, vo.getPageNum(), vo.getPageSize());
    }
 
    //对列表进行分页
    private TableDataInfo paginate(List list, int pageNum, int pageSize) {
        if (list == null || list.isEmpty()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        int total = list.size();
        int formIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(formIndex + pageSize, total);
        if (formIndex >= total) {
            return new TableDataInfo(new ArrayList<>(), total);
        }
        List subList = list.subList(formIndex, toIndex);
        return new TableDataInfo(subList, total);
    }
 
}