public void export(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { //写入台账信息到excel ,下载文件到zip List> dataList = new ArrayList() {{ add(new HashMap() {{ put("archiveNo", "0001-2010-Y-0004"); put("name", "区档案局传达学习“‘四责协同’机制建设推进会”精神"); put("code", "0001"); }}); add(new HashMap() {{ put("archiveNo", "0001-2010-Y-0003"); put("name", "加强纪律教育 强化规矩意识 营造知敬畏、存戒惧、守底线的良好氛围"); put("code", "0001"); }}); }}; Map contract = dataList.get(0); String fileName = contract.get("archiveNo") + ".zip"; // 创建临时文件 File zipFile; try { zipFile = File.createTempFile(contract.get("archiveNo") + "等" + dataList.size() + "条记录", ".zip"); FileOutputStream f = new FileOutputStream(zipFile); // 作用是为任何OutputStream产生校验和 //第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种) CheckedOutputStream checkedOutputStream = new CheckedOutputStream(f, new Adler32()); // 用于将数据压缩成Zip文件格式 ZipOutputStream zos = new ZipOutputStream(checkedOutputStream); HSSFWorkbook workbook = new HSSFWorkbook(); //创建工作表对象 HSSFSheet sheet = workbook.createSheet(); //创建工作表的行 //设置第一行,从零开始 HSSFRow row = sheet.createRow(0); //第一行第一列为日期 这里的列 后面再加 row.createCell(0).setCellValue("档号"); row.createCell(1).setCellValue("题名"); row.createCell(2).setCellValue("合同编号"); //设置sheet的Name workbook.setSheetName(0, "条目"); for (int k = 0; k < dataList.size(); k++) { Map contract2 = dataList.get(k); HSSFRow row1 = sheet.createRow(k + 1); row1.createCell(0).setCellValue(contract2.get("archiveNo")); row1.createCell(1).setCellValue(contract2.get("name")); row1.createCell(2).setCellValue(contract2.get("code")); //-------- 写入数据到excel end //以档号命名文件夹 zos.putNextEntry(new ZipEntry(contract2.get("archiveNo") + "\")); String filePath = contract2.get("archiveNo") + "\" + "文件" + "\"; zos.putNextEntry(new ZipEntry(filePath)); // List> maps1 = contractFileService.selectByContractId(id); List> maps1 = new ArrayList(){{ add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\开放鉴定WBS计划.xlsx"); }}); add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\附件7:市外差旅费报销单.xlsx"); }}); add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\附件6:市外差旅费报销单.xlsx"); }}); add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\开放鉴定相关sql.txt"); }}); }}; for (int i = 0; i < maps1.size(); i++) { Map m = maps1.get(i); String path = (String) m.get("path"); //name 后面追加 变成唯一名字 String name = path.substring(path.lastIndexOf("\") + 1); //在挡号下面创建文件 zos.putNextEntry(new ZipEntry(filePath + name)); InputStream inputStream = new FileInputStream(path); int bytesRead = 0; // 向压缩文件中输出数据 while ((bytesRead = inputStream.read()) != -1) { zos.write(bytesRead); } inputStream.close(); } //写入数据到 档号\附件 这个文件夹里 // List> maps2 = contractAttachmentService.selectByContractId(id); List> maps2 = new ArrayList(){{ add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\开放鉴定WBS计划.xlsx"); }}); add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\附件7:市外差旅费报销单.xlsx"); }}); add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\附件6:市外差旅费报销单.xlsx"); }}); add(new HashMap(){{ put("path", "C:\Users\Barlow\Desktop\开放鉴定相关sql.txt"); }}); }}; String attachmentPath = contract2.get("archiveNo") + "\" + "附件" + "\"; for (Map m : maps2) { String path = (String) m.get("path"); String name = path.substring(path.lastIndexOf("\") + 1); zos.putNextEntry(new ZipEntry(attachmentPath + name)); InputStream inputStream = new FileInputStream(path); int bytesRead = 0; // 向压缩文件中输出数据 while ((bytesRead = inputStream.read()) != -1) { zos.write(bytesRead); } inputStream.close(); } // 当前文件写完 zos.closeEntry(); } zos.putNextEntry(new ZipEntry("条目.xls")); InputStream inputStream = new ByteArrayInputStream(workbook.getBytes()); int bytesRead; // 向压缩文件中输出数据 while ((bytesRead = inputStream.read()) != -1) { zos.write(bytesRead); } inputStream.close(); zos.close(); String header = httpServletRequest.getHeader("User-Agent").toUpperCase(); if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) { fileName = URLEncoder.encode(fileName, "utf-8"); //IE下载文件名空格变+号问题 fileName = fileName.replace("+", "%20"); } else { fileName = new String(fileName.getBytes(), "ISO8859-1"); } httpServletResponse.reset(); httpServletResponse.setContentType("application/octet-stream; charset=utf-8"); httpServletResponse.setHeader("Location", fileName); httpServletResponse.setHeader("Cache-Control", "max-age=0"); httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + fileName); FileInputStream fis = new FileInputStream(zipFile); BufferedInputStream buff = new BufferedInputStream(fis); BufferedOutputStream out = new BufferedOutputStream(httpServletResponse.getOutputStream()); byte[] car = new byte[4096]; int l = 0; while (l < zipFile.length()) { int j = buff.read(car, 0, 1024); l += j; out.write(car, 0, j); } // 关闭流 fis.close(); buff.close(); out.close(); // 删除临时文件 zipFile.delete(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
上一篇 zookeeper安装(基于ubuntu20.04版本)
下一篇 数据分析|SQL基础自学笔记(查询)
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号