后端打包压缩包代码,前端接收响应下载

//临时工workbooks.zip文件
File zipFile = File.createTempFile("workbooks", ".zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));


for(..........){
   
	//临时workbook.xlsx文件,workbook写入xlsx中
	File tempFile = File.createTempFile(fileName, ".xlsx");
	try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(tempFile))) {
   
	    workbook.write(bufferedOutputStream);
	}
	
	//写入zip文件中
	File file = tempFile;
	byte[] buffer = new byte[1024];
	try (FileInputStream fileInputStream = new FileInputStream(file)) {
   
	    ZipEntry zipEntry = new ZipEntry(entryName);
	    zipOutputStream.putNextEntry(zipEntry);
	    int len;
	    while ((len = fileInputStream.read(buffer)) > 0) {
   
	        zipOutputStream.write(buffer, 0, len);
	    }
	    zipOutputStream.closeEntry();
	}
}
zipOutputStream.close();
//写出到客户端下载
HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=workbooks.zip");
        InputStreamResource inputStreamResource = new InputStreamResource(new FileInputStream(zipFile));


return ResponseEntity
        .ok()
        .headers(headers)
        .contentType(MediaType.APPLICATION_OCTET_STREAM)
        .body(inputStreamResource);

客户端接收响应下载文件

axios.post(
	url, //自己填
	data,//自己填
	.... 
	{
   responseType: 'blob'}).then((res) => {
   
		this.visible_mergeMax_modal = false;
		const blob = new Blob([res], {
    type: 'application/zip' });
		const url = window.URL.createObjectURL(blob);
		const link = document.createElement('a');
		link.href = url;
		link.setAttribute('download', 'yourfilename'+this.getCurrentDate()+'.zip');
		document.body.appendChild(link);
		link.click();
		window.URL.revokeObjectURL(url);
		document.body.removeChild(link);
		}).catch((error) => {
   
		console.error('Error generating and downloading ZIP:', error);
})

相关推荐

  1. 打包压缩代码,前端接收响应下载

    2023-12-21 11:26:02       69 阅读
  2. 下载文件 返回给前端 response header 响应

    2023-12-21 11:26:02       70 阅读
  3. 前端接收传的文件流并下载解决乱码问题

    2023-12-21 11:26:02       62 阅读
  4. axios下载接口返回了json但前端得到的是blob

    2023-12-21 11:26:02       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 11:26:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 11:26:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 11:26:02       87 阅读
  4. Python语言-面向对象

    2023-12-21 11:26:02       96 阅读

热门阅读

  1. 12.20力扣

    2023-12-21 11:26:02       68 阅读
  2. 龙芯loongarch64服务器编译安装paddlepaddle

    2023-12-21 11:26:02       74 阅读
  3. AWS认证SAA-C03每日一题

    2023-12-21 11:26:02       45 阅读
  4. 移动端1像素的解决方案?

    2023-12-21 11:26:02       71 阅读
  5. Springboot集成JPA多Hibernate数据源

    2023-12-21 11:26:02       54 阅读
  6. display:grid

    2023-12-21 11:26:02       72 阅读
  7. 使用IntelliJ IDEA进行Python开发配置

    2023-12-21 11:26:02       62 阅读
  8. 进程间通讯-信号量

    2023-12-21 11:26:02       59 阅读
  9. stable diffusion 极简入门 核心 概念介绍 使用

    2023-12-21 11:26:02       57 阅读