/** * 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; }