Java Zip压缩文件返回前端并下载

ZIP工具类:

复制代码
@Slf4j
public class ZipUtils {

    /**
     * 将多个流转成zip文件输出
     * @param listStream 文件流实体类对象
     * @param fileName zip包的名称
     * @param response
     * @return
     */
    public static boolean listStreamToZipStream(List<ZipDto> listStream, String fileName, HttpServletResponse response) {
        boolean flag = false;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.reset();
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
            response.setHeader("Access-Control-Allow-Origin","*");
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            zos = new ZipOutputStream(out);
            byte[] bufs = new byte[1024 * 10];
            for (ZipDto zipDto : listStream) {
                String streamfilename = zipDto.getName();
                // 创建ZIP实体,并添加进压缩包
                ZipEntry zipEntry = new ZipEntry(streamfilename);
                zos.putNextEntry(zipEntry);
                // 读取待压缩的文件并写进压缩包里
                bis = new BufferedInputStream(zipDto.getInputstream(), 1024 * 10);
                int read = 0;
                while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                    zos.write(bufs, 0, read);
                }
            }
            flag = true;
            zos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            // 关闭流
            try {
                if (null != bis){
                    bis.close();
                }
                if (null != zos){
                    zos.close();
                }
                if (null != out){
                    out.close();
                }
            } catch (IOException e) {
//                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
        return flag;
    }

}
复制代码

ZIP DTO实体类:

复制代码
@Data

public class ZipDto {
    public String name;
    public InputStream inputstream;
    
    public ZipDto(String name, InputStream inputstream) {
        this.name = name;
        this.inputstream = inputstream;
    }
}
复制代码

ZIP 方法层:

复制代码
public Boolean exportFile(List<String> fileNames, HttpServletResponse response, HttpServletRequest request) {
QueryWrapper<查询数据> queryWrapper = new QueryWrapper();
queryWrapper.in("file_name",fileNames);
List<查询数据> cmtDealerSpotplanManagements = list(queryWrapper);
  // 将数据处理
List<String> spotPlanNames = cmtDealerSpotplanManagements.stream().map(查询数据::getFilePath).collect(Collectors.toList());
List<ZipDto> list = new ArrayList<>();
  // 数据为空则返回
if ( CollectionUtils.isEmpty(spotPlanNames) ){
return false;
}
  // 将数据地址去远程下载
for (String fileUrl : spotPlanNames){
File tempFile = new File(fileUrl);
String newFileUrl = fileUrl.replace(" ", "%20");
InputStream fileInputStream = fileService.download2(newFileUrl);
String tempFileName = tempFile.getName();
list.add(new ZipDto(tempFileName, fileInputStream));
}
String fileName = "test" + ".zip";
ZipUtils.listStreamToZipStream(list, fileName, response);
return true;
}
复制代码

 

posted @   曹丽是我女朋友。  阅读(6707)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示