springboot-vue导出压缩包
后端
controller
@Operation(summary = "采购单-导出合同压缩包", description = "采购单-导出合同压缩包")
@GetMapping("/imgZip")
public void impZip(S_JinchuHuodan sJinchuHuodan, @Schema(hidden = true) HttpServletResponse response) throws IOException {
List<Integer> ids = sJinchuHuodan.getIds();
File file = new File("src/main/resources/files/zip");
List<String> list = jinchuHuodanService.getHeTongByCaiGouIds(ids);
FileOSSZIPUtil.downloadOSS(list);
File zip = ZipUtil.zip(file);
FileInputStream fileInputStream = new FileInputStream(zip);
response.setHeader("content-disposition", "attachment;filename=" + new String("合同".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + ".zip");
response.setHeader("responseType", "file");
//压缩文件
int read = fileInputStream.available();
long copy = IoUtil.copy(fileInputStream, response.getOutputStream());
if (read == copy) {
fileInputStream.close();
file = new File("src/main/resources/files");
FileUtil.del(file);
}
}
工具
/**
* 将OSS文件下载到本地
*/
@Slf4j
public class FileOSSZIPUtil {
/**
* 从OSS服务中下载所需文件到本地临时文件
*
* @param objectNames 要下载的对象/文件
* @return
*/
public static void downloadOSS(List<String> objectNames) {
//创建文件
File file = new File("src/main/resources/files/zip");
if (!file.exists()) {
file.mkdirs();
}
//图片下载到本地
for (String objectName : objectNames) {
try {
@Cleanup InputStream inputStream = new URL(objectName).openStream();
//处理老图片
if (objectName.contains("token")) {
String imgUrl = StringUtil.sub(objectName, objectName.lastIndexOf("."), objectName.lastIndexOf("?"));
@Cleanup FileOutputStream downloadFile = new FileOutputStream("src/main/resources/files/zip/" + new Snowflake().nextIdStr() + imgUrl);
IoUtil.copy(inputStream, downloadFile);
continue;
}
int i = objectName.lastIndexOf("/");
String imgUrl = StringUtil.sub(objectName, i, objectName.length());
@Cleanup FileOutputStream downloadFile = new FileOutputStream("src/main/resources/files/zip/" + imgUrl);
IoUtil.copy(inputStream, downloadFile);
} catch (IOException e) {
log.info("文件{}下载失败", objectName);
}
}
}
}
前端
方法
//导出合同
handleImgZip() {
toImgZip({ids:this.idList.toString()}).then(res => {
this.exportFile2(res, '合同.zip');
})
this.loading = false;
},
exportFile2(res, fileName) {
this.loading = true;
const blob = new Blob([res.data], { type: 'application/zip' });
const url = window.URL.createObjectURL(blob); //从blob对象获取url
const link = document.createElement('a') // 创建a标签
link.style.display = 'none' // 设置a标签样式不可见
link.href = url // 设置a标签href属性
link.download = fileName // 设置文件下载名称
link.click() // 点击a标签
link.remove() // 删除a标签
window.URL.revokeObjectURL(url); // 释放掉blob对象
this.loading = false;
},
// 多选选中表格
handleSelectionChange(val) {
console.log('val', val);
if (!val.length) {
this.idList = []
return
}
this.supplierssId = val[0].supplierssId
this.projectId = val[0].projectId
this.idList = []
//val是选中的数据的数组
val.forEach(item => {
const id = item.jinchuHuodanId
//判断数组中是否包含某个值
if (this.idList.indexOf(id) === -1) {
this.idList.push(id)
}
})
console.log(this.idList);
},
<el-button type="primary"
icon="el-icon-search"
class="has-after-button"
@click="handleImgZip()">导出合同</el-button>