print
?
注意在Controller里必须使用ResourceRequest和ResourceResponse来获取输出流
需要注意的是,jsp页面上用href来请求下载功能
- @RequestMapping(params = "action=getPDF")
- public void getPDF(ResourceRequest request, ResourceResponse response)
- throws Exception {
- String pdfType = request.getParameter("pdfType");
- response.reset();
- response.setContentType("application/pdf");
- String attachmentFilename = "";
- int modelId = 0;
- if (pdfType.equals(MailTemplate.TYPE_PAYMENT)) {
- modelId = regConfig.getReceiptId();
- attachmentFilename = "attachment; filename=\"receipt.pdf" + "\"";
- } else if (pdfType.equals(MailTemplate.TYPE_INVITATION)) {
- modelId = regConfig.getInvitationId();
- attachmentFilename = "attachment; filename=\"invitation.pdf" + "\"";
- } else {
- return;
- }
- response.setProperty("Content-disposition", attachmentFilename);
- String filepath = defaultTemplateContext.getDefaultDir()
- + File.separator;
- filepath += MailTemplate.DEF_INVITATION_MODEL_PDF;
- ByteArrayOutputStream ous = null;
- InputStream ins = null;
- try {
- Map<String, Object> data = 。。。。。。;
- PDFTemplatePrinter rprinter = new PDFTemplatePrinter(filepath);
- ous = rprinter.getPDFOutpuStream(data);
- ins = new ByteArrayInputStream(ous.toByteArray());
- int b = -1;
- while ((b=ins.read())!=-1) {
- out.write(b);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- ins.close();
- ous.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }