springmvc itext 生成pdf文档下载的时候只能用浏览器自动查看

cqwck 发布于 2016/01/14 14:09
阅读 1K+
收藏 0

【深度】AI、5G时代下,算力网络与内生安全最全解析!>>>

我用springmvc+itext生成pdf文档的时候,在有某些时候下载的时候只能用浏览器浏览不能自动下载代码如下:真没看出什么问题,有遇到过类似问题并解决的朋友分享下,感激

着是controller

/**
* 下载多个PDF文档
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping("downloadPdfs")
public ModelAndView viewPDFs(String[] serviceNos, HttpServletResponse response) throws Exception {
Map<String,Object> model = new HashMap<String,Object>();
List<PDFContentAction> pDFContentActions = new ArrayList<PDFContentAction>();
for (int i = 0; i < serviceNos.length; i++) {
PDFContentAction pDFContentAction = new PDFContentAction();
String serviceNo = serviceNos[i];
ApiServiceDoc service = apiServiceDocService.findByServiceNo(serviceNo);
Map<String, String> requesetMsgDemos = new HashMap<>();
requesetMsgDemos.put("MD5Request",
generateMessageDemo(service, MessageType.Request, DigestUtil.DigestALGEnum.MD5));
requesetMsgDemos.put("MD5Response",
generateMessageDemo(service, MessageType.Response, DigestUtil.DigestALGEnum.MD5));
requesetMsgDemos.put("MD5Notify",
generateMessageDemo(service, MessageType.Notify, DigestUtil.DigestALGEnum.MD5));
requesetMsgDemos.put("SHA256Request",
generateMessageDemo(service, MessageType.Request, DigestUtil.DigestALGEnum.SHA256));
requesetMsgDemos.put("SHA256Response",
generateMessageDemo(service, MessageType.Response, DigestUtil.DigestALGEnum.SHA256));
requesetMsgDemos.put("SHA256Notify",
generateMessageDemo(service, MessageType.Notify, DigestUtil.DigestALGEnum.SHA256));
pDFContentAction.setRequesetMsgDemos(requesetMsgDemos);
pDFContentAction.setService(service);
pDFContentActions.add(pDFContentAction);
}
model.put("pDFContentActions", pDFContentActions);
return new ModelAndView(new ViewPDF(), model);
}

这是重写的方法

 */
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {
@SuppressWarnings("unchecked")
List<PDFContentAction> pDFContentActions = (List<PDFContentAction>) model.get("pDFContentActions");
OutputStream outs = response.getOutputStream(); // 获取输出流
writer = PdfWriter.getInstance(document, outs);
writer.setPageEvent(new PdfWatermark("易极付开放平台"));//水印内容 
PdfHeaderFooter headerFooter = new PdfHeaderFooter();
Rectangle rect = new Rectangle(36, 54, 559, 788);
rect.setBorderColor(BaseColor.BLACK);
writer.setBoxSize("art", rect);
writer.setPageEvent(headerFooter);
document.open(); // 打开 Document 文档  
int i = 1;
for (PDFContentAction pDFContentAction : pDFContentActions) {
ApiServiceDoc service = pDFContentAction.getService();
Map<String, String> msdDemosMap = pDFContentAction.getRequesetMsgDemos();
if (service != null) {
//标题
Paragraph headTitle = new Paragraph(service.getServiceName(), headfont);
headTitle.setAlignment(Element.ALIGN_CENTER);
headTitle.setSpacingAfter(15f);
headTitle.setSpacingBefore(15f);
//设置章节目录
Chapter chapter = new Chapter(headTitle, i);
document.add(chapter);
i++;
//服务码
Paragraph serviceNo = new Paragraph("服务码:" + service.getService(), textfont);
serviceNo.setAlignment(Element.ALIGN_LEFT);
serviceNo.setSpacingBefore(15f);
//版本号
Paragraph serviceVsersion = new Paragraph("版本号:" + service.getVersion(), textfont);
serviceVsersion.setAlignment(Element.ALIGN_LEFT);
serviceVsersion.setSpacingBefore(15f);
//通讯方式
Paragraph serviceType = new Paragraph("通讯方式:" + service.getServiceType(), textfont);
serviceType.setAlignment(Element.ALIGN_LEFT);
serviceType.setSpacingBefore(15f);
//通讯协议
Paragraph serviceWay = new Paragraph("通讯协议:HTTP POST/GET", textfont);
serviceWay.setAlignment(Element.ALIGN_LEFT);
serviceWay.setSpacingBefore(15f);
//正式环境联调网关
Paragraph proEnvi = new Paragraph("正式环境接口网关:http://openapi.yiji.com/gateway.html", textfont);
proEnvi.setAlignment(Element.ALIGN_LEFT);
proEnvi.setSpacingBefore(15f);
//联调环境联调网关
Paragraph alignEnvi = new Paragraph("联调环境接口网关:http://openapi.yijifu.net/gateway.html", textfont);
alignEnvi.setAlignment(Element.ALIGN_LEFT);
alignEnvi.setSpacingBefore(15f);
//功能描述
Paragraph funDescr = new Paragraph(service.getManualNote(), textfont);
funDescr.setAlignment(Element.ALIGN_LEFT);
funDescr.setSpacingBefore(15f);

//API参数
Paragraph apiPara = new Paragraph("API参数", secondfont);
apiPara.setAlignment(Element.ALIGN_LEFT);
apiPara.setSpacingBefore(18f);
document.add(serviceNo);
document.add(serviceVsersion);
document.add(serviceType);
document.add(serviceWay);
document.add(proEnvi);
document.add(alignEnvi);
document.add(funDescr);
//添加API参数
addTable(document, service);
//添加示例
addExample(document, msdDemosMap);
} else {
// pdf 文件中的一个文字段落  
Paragraph paragraph = new Paragraph("空文档,请重新下载!");
// 添加一个文字段落、一张图片  
document.add(paragraph);
}
}
// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开  
document.close();
//文件名称
String pdfName = "openapi服务详情.pdf";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setDateHeader("Expires", System.currentTimeMillis()+30000);
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(pdfName.getBytes("gb2312"), "ISO8859-1"));
}


加载中
返回顶部
顶部