/** * excel下载 * * @param billIdStr * @return * @throws Exception */ @GET @Path("/downloadPdf") @SuppressWarnings("unchecked") @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response downloadExcel(final @QueryParam("billIdStr") String billIdStr) throws Exception { Map<String, Integer> objMap = (Map<String, Integer>) queryParmas; if (objMap == null || objMap.isEmpty()) { return null; } List<Integer> billIdList = new ArrayList<Integer>(); Integer billId = null; if (objMap.size() == 1) { billId = objMap.get(String.valueOf(0)); return downloadOnePdf(billId); } else if (objMap.size() > 1) { for (int i = 0; i < objMap.size(); i++) { Integer id = objMap.get(String.valueOf(i)); billIdList.add(id); } return downloadMorePdf(billIdList); } else { logger.info(objMap.size()); return null; } }
private Response downloadMorePdf(List<Integer> billIdList) throws Exception { Response response = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); List<String> list = new ArrayList<String>(); for (int i = 0; i < billIdList.size(); i++) { Integer billId = billIdList.get(i); Map<String, Object> templateMap = getTemplateMap(billId); if (templateMap == null || templateMap.isEmpty()) { logger.info("账单数据不完整,无法下载账单"); continue; } StringBuffer fileNameStr = new StringBuffer(); Bill bill = billMapper.selectByPrimaryKey(billId); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); fileNameStr .append(bill.getAgencyNickName()) .append(sdf.format(bill.getBillStartTime())) .append("-") .append(sdf.format(bill.getBillEndTime())) .append(".") .append(".pdf"); // classpath 中模板路径,根据html模版来生成pdf String template = "pdfConfig/templates/test.html"; PdfDocumentGenerator pdfGenerator = new PdfDocumentGenerator(); // 生成pdf try { pdfGenerator.generate(template, getTemplateMap(billId), bos); } catch (DocumentGeneratingException e) { e.printStackTrace(); } list.add(fileNameStr.toString()); } zipFiles(list, bos); String zipFileName = date2String(new Date()) + ".zip"; response = Response.status(Status.OK) .header("Content-Type", "application/zip;charset=ISO8859-1") .header("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"") .entity(bos.toByteArray()) .build(); return response; }
private void zipFiles(List<String> srcfile, ByteArrayOutputStream bos) { try { ZipOutputStream out = new ZipOutputStream(bos); out.setEncoding("GBK"); for (int i = 0; i < srcfile.size(); i++) { out.putNextEntry(new ZipEntry(srcfile.get(i))); out.write(bos.toByteArray()); } out.closeEntry(); out.flush(); out.finish(); out.close(); } catch (IOException e) { e.printStackTrace(); logger.error("ZipUtil zipFiles exception:" + e); } }
private Map<String, Object> getTemplateMap(Integer billId) { Map<String, Object> root = new HashMap<String, Object>(); PreviewInfoVO previewInfoVO = billMapper.selectPreviewInfo(billId); if (previewInfoVO == null) { return root; } previewInfoVO.setNullFields(); root.put("previewInfoVO", previewInfoVO); BillInformationExample billInformationExample = new BillInformationExample(); billInformationExample.createCriteria().andBillIdEqualTo(billId); billInformationExample.setOrderByClause("id asc"); List<BillInformation> billInformationList = billInformationMapper.selectByExample(billInformationExample); if (billInformationList != null && billInformationList.size() == 4) { // billInformationList = new ArrayList<BillInformation>(); // BillInformation info1 = new BillInformation(); // info1.setFeeType("流量费"); // billInformationList.add(info1); // // BillInformation info2 = new BillInformation(); // info2.setFeeType("服务费"); // billInformationList.add(info2); // // BillInformation info3 = new BillInformation(); // info3.setFeeType("名单费"); // billInformationList.add(info3); // // BillInformation info4 = new BillInformation(); // info3.setFeeType("其他费用"); // billInformationList.add(info4); } Integer consumeNumber = billInformationList.get(0).getConsumeNumber(); consumeNumber = consumeNumber == null ? new Integer(0) : consumeNumber; billInformationList.get(0).setConsumeNumber(consumeNumber); root.put("info1", billInformationList.get(0)); root.put("info2", billInformationList.get(1)); root.put("info3", billInformationList.get(2)); root.put("info4", billInformationList.get(3)); root.put("imagePath", ResourceLoader.getPath("pdfConfig/images")); return root; }