资源打包成ZIP下载
框架添加了将资源打包成ZIP并下载的功能,主版本RSP框架已包含此功能,老版本框架若要使用,请进行如下操作,集成功能:
# 一. 后端
# 1. 新增SysDownload.java
在[项目]-common 模块中,com.risun.common.core.domain.entity 包下,创建 SysDownload.java,内容如下
package com.risun.common.core.domain.entity;
import javax.validation.constraints.NotBlank;
import com.risun.common.core.domain.BaseEntity;
/**
* 通用资源下载请求对象
*
* @author dante
*/
public class SysDownload extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 本地资源(文件的路径)
*/
@NotBlank(message = "本地资源不能为空")
private String resource;
/**
* 下载文件名(名称+后缀)
*/
@NotBlank(message = "下载文件名不能为空")
private String fileName;
public String getResource() {
return resource;
}
public void setResource(String resource) {
this.resource = resource;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Override
public String toString() {
return "SysDownload [resource=" + resource + ", fileName=" + fileName + "]";
}
}
# 2. 新增ZipDownloadUtil.java
在[项目]-common 模块中,com.risun.common.utils 包下,创建 ZipDownloadUtil.java,内容如下
package com.risun.common.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.risun.common.constant.Constants;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
/**
* 资源打包为 ZIP 并下载
*
* @author dante
*
*/
@Slf4j
public class ZipDownloadUtil {
public static void zip(String baseUrl, String resource, OutputStream outputStream) {
String[] resourceArr = resource.split(",");
int len = resourceArr.length;
String[] fileNameArr = new String[len];
InputStream[] insArr = new FileInputStream[len];
for (int i = 0; i < len; i++) {
fileNameArr[i] = FileUtil.getName(resourceArr[i]);
insArr[i] = getFileInputStream(baseUrl + StringUtils.substringAfter(resourceArr[i], Constants.RESOURCE_PREFIX));
}
ZipUtil.zip(outputStream, fileNameArr, insArr);
}
private static FileInputStream getFileInputStream(String filePath) {
FileInputStream fis = null;
try {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
fis = new FileInputStream(file);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return fis;
}
}
# 3. 修改CommonController.java
CommonController.java 位于[项目]-admin 模块,com.risun.web.controller.common 包下。添加如下内容:
/**
* 本地资源打包成ZIP并下载
*/
@PostMapping("/download/resource2zip")
public void resource2ZipDownload(@Validated SysDownload sysDownload, HttpServletRequest request, HttpServletResponse response) {
try {
String resource = sysDownload.getResource();
String downloadName = sysDownload.getFileName();
if (!FileUtils.checkAllowDownload(downloadName)) {
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", downloadName));
}
// 本地资源路径
String localPath = RisunConfig.getProfile();
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, downloadName);
ZipDownloadUtil.zip(localPath, resource, response.getOutputStream());
} catch (Exception e) {
log.error("下载文件失败", e);
}
}
# 二. 前端
修改 download.js(位于 [项目]-ui/src/plugins 下),添加如下内容:
import { param } from "@/utils/index"
resource2zip(query) {
var url = baseURL + "/common/download/resource2zip"
axios({
method: 'post',
url: url,
responseType: 'blob',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + getToken()
},
data: param(query)
}).then(async (res) => {
const isLogin = await blobValidate(res.data);
if (isLogin) {
const blob = new Blob([res.data])
this.saveAs(blob, decodeURI(res.headers['download-filename']))
} else {
this.printErrMsg(res.data);
}
})
},
# 三. 使用说明
/**
* resource: 要被打包的文件路径,用逗号分隔
* fileName: 打包后zip的名称
*/
this.$download.resource2zip({ resource: "filePath", fileName: "xx.zip"})