wujianwei
2025-08-14 22976afc564c4b5b911026c6540ad48db43e2166
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.ruoyi.common.utils.file;
 
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ZipUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/**
 * 导出下载文件
 */
public class DownloadExportUtil {
    protected static final Logger logger = LoggerFactory.getLogger(DownloadExportUtil.class);
    public enum ExprotStatus{
        XZZ("0","下载中"),
        FQXZ("1","放弃下载"),
        XZYC("-1","数据导出失败"),
        ;
        private String status;
        private String desc;
 
        private ExprotStatus(String status,String desc){
            this.status=status;
            this.desc = desc;
        }
        public String getStatus() {
            return status;
        }
 
        public String getDesc() {
            return desc;
        }
    }
    /**
     * 获取导出数据key
     * @param key
     * @return
     */
    public static String getExprotKey( String key){
        return Constants.EXPORT_PROFILE+":"+key;
    }
    /**
     * 清除导出任务
     * @param redisCache
     * @param key
     * @return
     */
    public static void clearDownload(RedisCache redisCache, String key) {
        deleteDownloadFile(redisCache,key, ExprotStatus.FQXZ.status);
    }
    /**
     * 删除下载的文件
     * @param key
     * @param value 设置的值,空直接删除
     */
    public static void deleteDownloadFile(RedisCache redisCache,String key,String value){
        try {
            String val =redisCache.getCacheObject(Constants.EXPORT_PROFILE+":"+key);
            if(value==null) {
                redisCache.deleteObject(Constants.EXPORT_PROFILE  + ":" + key);
            }else{
                redisCache.setCacheObject(Constants.EXPORT_PROFILE  + ":" + key,value,10,TimeUnit.MINUTES);
            }
            if(StringUtils.isEmpty(val)) {
                String filePath = RuoYiConfig.getDownloadPath() + val;
                FileUtils.deleteFile(filePath);
            }
        }catch (Exception e) {
            logger.error("异常",e);
        }
    }
    /**
     * 设置下载的文件
     * @param key
     */
    public static void setDownloadFile(RedisCache redisCache, String key,String fileName){
        try {
            redisCache.setCacheObject(Constants.EXPORT_PROFILE+":"+key,fileName,10, TimeUnit.MINUTES);
        }catch (Exception e) {
            logger.error("异常",e);
        }
    }
    public static String setZipFile(String fileName,List<String> fileNames){
        if(fileNames.size()==0){
            return fileName;
        }
        fileName = fileName.replace("xlsx","zip");
        ZipUtils.generatorZipFile(fileName,fileNames,true);
        return fileName;
    }
 
    /**
     * 设置下载的文件
     * @param exportKey
     */
    public static String setDownloadFile(RedisCache redisCache,  String exportKey, String fileName, List<String> fileNames){
        try {
            //直接生成一个表格
            if(fileNames.size()==0){
                //设置下载文件
                setDownloadFile(redisCache,  exportKey, fileName);
                return fileName;
            }
 
            //region 压缩处理
            fileName=fileName.substring(0,fileName.lastIndexOf("."))+".zip";
            ZipUtils.generatorZipFile(fileName,fileNames,true);
            setDownloadFile(redisCache,  exportKey, fileName);
            //endregion
        }catch (Exception e) {
            logger.error("异常",e);
        }
        return fileName;
    }
 
    /**
     * 获取下载文件地址
     * @param redisCache
     * @param key
     * @return
     */
    public static String getDownloadFileName(RedisCache redisCache, String key){
        try {
            String redisKey = getExprotKey(key);
            return redisCache.getCacheObject(redisKey);
        }catch (Exception e) {
            logger.error("异常",e);
            return null;
        }
    }
    /**
     * 下载文件
     * @param fileName
     * @param delete
     */
    public static void fileDownload(String fileName, Boolean delete)
    {
        try
        {
            HttpServletRequest request=  ServletUtils.getRequest();;
            HttpServletResponse response=ServletUtils.getResponse();
            if (!FileUtils.isValidFilename(fileName))
            {
                throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
            }
            String num = RandomUtils.random(2);
            String realFileName = fileName.substring(fileName.indexOf("_") + 1).replace(".",num+".");
            String filePath = RuoYiConfig.getDownloadPath() + fileName;
 
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (delete)
            {
                FileUtils.deleteFile(filePath);
            }
        }
        catch (Exception e)
        {
            logger.error("下载文件失败", e);
        }
    }
 
}