具体导出word功能和下图的downLoadFile方法,请参考:https://blog.csdn.net/sumengnan/article/details/105947476

下面主要说的是压缩zip文件。

    /**
     * 导出全部简历
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/exportAllResume", method = {org.springframework.web.bind.annotation.RequestMethod.GET, org.springframework.web.bind.annotation.RequestMethod.POST})
    public void exportAllResume(@ModelAttribute AdjustAbilityPersonModel model, HttpServletResponse response, @CookieValue(value = SessionManager.SESSION_NAME) String sessionId) throws Exception {
        ZipOutputStream zos=null;
        ByteArrayOutputStream arrayOutputStream=null;
        try {
            response.reset();
            response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
            response.setHeader("Content-Disposition", "attachment;filename=" +new String("全部简历".getBytes("GB2312"),"iso-8859-1")+".zip");
            zos = new ZipOutputStream(response.getOutputStream());//压缩文件输出流
            arrayOutputStream= new ByteArrayOutputStream();//ByteArray临时存储流
            //查询要导出的列表
            SysUser user = userSessionManager.getUser(sessionId);
            List<AdjustAbilityPerson> list = adjustAbilityPersonService.findAll(model);
            for (AdjustAbilityPerson bean : list) {
                try {
                    XWPFDocument xwpfDocument = this.downLoadFile(response, bean, templateDir, user);
                    //获取已经替换好的word对象。downLoadFile方法,请参考:https://blog.csdn.net/sumengnan/article/details/105947476 第四步
                    String sysGroup=(bean.getSysGroup() != null?bean.getSysGroup().getName():"");
                    String userName=(bean.getUserName() != null?bean.getUserName():"");
                    zos.putNextEntry(new ZipEntry(sysGroup+"-"+userName+"简历.docx"));
                    //放入zipEntry实体,取一个名字
                    arrayOutputStream.reset();//重置ByteArray流(为了重复使用)
                    xwpfDocument.write(arrayOutputStream);//把word对象内容写到ByteArray流临时存储
                    zos.write(arrayOutputStream.toByteArray());//把ByteArray流内容写入zip输出流
                } catch (Exception e) {
                    log.warn("导出简历失败id:"+bean.getId(),e);
                }
            }
        } catch (Exception e) {
            log.error("导出全部简历失败",e);
        } finally {
            if(zos!=null){
                zos.closeEntry();//关闭zipEntyp
                zos.flush();//刷新zip输出流缓冲区
                zos.close();//关闭zip输出流
            }
            if(arrayOutputStream!=null){
                arrayOutputStream.close();//关闭ByteArray流
            }
        }
    }

 

 

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐