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 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 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); } } }