java实现文件打包下载

/**
     * 根据url打包下载
     * @param response
     * @throws IOException
     */
public void downloadZipFile(HttpServletResponse response) {
        try {
            String filename ="文件名";
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
            response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".zip");

            //查询报关单号的全部附件
            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());

            for (BillFileViewVo billFileViewVo : billFileVo.getBillFileViews()) {
                ZipEntry zipEntry = new ZipEntry(billFileViewVo.getOriginfileName());
                zipOutputStream.putNextEntry(zipEntry);
                // 构造URL
                URL url = new URL(billFileViewVo.getAliyunFileUrl());
                // 打开连接
                URLConnection con = url.openConnection();
                //设置请求超时为10s
                con.setConnectTimeout(10 * 1000);
                // 输入流
                InputStream is = con.getInputStream();
                IOUtils.copy(is, zipOutputStream);
                is.close();
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();


        } catch (Exception e) {
            e.printStackTrace();

        }

    }




/**
     * 根据模板生成文件打包下载
     *
     * @param response
     * @throws IOException
     */
    public void downloadZipFile(HttpServletResponse response) throws IOException {

        try {
            String filename = "文件名";
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
            response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1") + ".zip");
            //查询报关单号的全部附件
            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());

            for (int i = 0; i < 2; i++) {
                TemplateExportParams params = new TemplateExportParams("templates/12仓.xlsx", true);
                Map<String, Object> map = new HashMap<>();
                //往map添加你要生成的数据
                Workbook wb = ExcelExportUtil.exportExcel(params, map);
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ByteArrayInputStream is = null;

                try {
                    try {
                        wb.write(os);
                    } catch (IOException var21) {
                        var21.printStackTrace();
                    }

                    byte[] content = os.toByteArray();
                    is = new ByteArrayInputStream(content);
                } finally {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                InputStream in = is;
                ZipEntry zipEntry = new ZipEntry(i + ".xlsx");
                zipOutputStream.putNextEntry(zipEntry);
                // 输入流
                IOUtils.copy(in, zipOutputStream);
                in.close();
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值