多个流文件打包为ZIP

无需本地暂存文件,直接给流到前端

Controller

@PostMapping(value = "自己的请求地址")
    public byte[] downlondResume(HttpServletResponse response,@RequestBody List<String> personIds) throws Exception {
        byte[] responseEntity = personService.downloadAll(response, personIds);
        String fileName = URLEncoder.encode("批量导出.zip", "UTF-8");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        return responseEntity;
    }

Service

 byte[] downloadAll(HttpServletResponse response, List<String> personIds) throws ExecutionException, InterruptedException, Exception;

ServiceImp

 @Override
    public byte[] downloadAll(HttpServletResponse response, List<String> personIds) {
        Map<String, byte[]> byteFileMap = new HashMap<>();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
       //自己的取流接口
        batchFileToZIP(byteFileMap, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

	public void batchFileToZIP(Map<String, byte[]> byteList, ByteArrayOutputStream byteOutPutStream) {
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteOutPutStream);
        try {
            for (Map.Entry<String, byte[]> entry : byteList.entrySet()) {
                //写入一个条目,我们需要给这个条目起个名字,相当于起一个文件名称
                zipOutputStream.putNextEntry(new ZipEntry(entry.getKey()));
                zipOutputStream.write(entry.getValue());
            }
            zipOutputStream.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

大概流程就是这样,自己的是传的一组人员的id,根据人员的id进行数据的拼装
然后装到一个Map里面,设计为Map的key为传入人员的工号
value为传入人员的字节流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值