【发布时间】:2017-01-04 12:47:36
【问题描述】:
我正在创建用于以 pdf 格式下载我的报告的 api。 为此,我使用 java(spark 框架)和 jasper 报告工具 一切正常。
问题是当我从邮递员那里访问我的 api 时,PDF 的默认名称是“response.pdf.pdf”,我希望它是“report.pdf”
这是我的方法。
private Route getReport = (req,res)->{
try{
res.raw().setHeader("Content-Disposition", "attachment; filename= \"report.pdf\"");
res.raw().setContentType("application/pdf");
JRDataSource dataSource = new JREmptyDataSource();
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("cin", "556293-9982");
dataMap.put("financialYear","2009-05-01 - 2010-04-30");
JasperPrint jasperPrint = JasperFillManager.fillReport("reports/front-page.jasper", dataMap,dataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint,res.raw().getOutputStream());
}catch(Exception ex){
res.type(ApplicationConstants.JSON_APPLICATION_CONTENT_TYPE);
throw new BusinessExceptions(ex, ApiErrorEnumerations.ERR_DOWNLOADING_REPORT);
}finally{
res.raw().flushBuffer();
res.raw().getOutputStream().close();
}
return res.raw();
};
请指出我哪里出错了。
【问题讨论】:
-
@AlexK 我们必须在“exportReportToPdfStream”上编写报告,您的链接的解决方案是使用“exportReportToPdfFile”。
-
@AlexK 仍然没有运气。
-
:)
res的类型是什么?res.raw()是什么?未知代码很难帮你 -
感谢您的回复。 res.raw() 是“httpServletResponse”。我正在使用 java spark。
标签: java jasper-reports export-to-pdf