[英]How to make a pdf appear as a download option rather than rendering on the browser?
我在我的項目中使用Jasper Reports以多種格式生成報告。 盡管所有代碼示例運行良好,但我遇到了一個概念問題。
我寫了這個簡單的代碼,它在瀏覽器中生成PDF作為下載選項:
@GET
@Path("/basicdbreport")
@Produces("application/pdf")
public Response basicDbReport(@Context ServletContext context,
@Context HttpServletResponse response) throws JRException,
IOException {
Connection connection = null;
byte[] reportBytes = null;
String reportName = "firstsqlexample";
File file = new File(path + reportName + JASPER_EXTENSION);
// check if compiled report exists
if (!file.exists()) {
compileReport(context.getRealPath(path + reportName + JRXML_EXTENSTION));
}
// input stream for filling the compiled report
InputStream compiledReportStream = context.getResourceAsStream(path
+ reportName + JASPER_EXTENSION);
try {
connection = dataSource.getConnection();
reportBytes = JasperRunManager.runReportToPdf(compiledReportStream,
new HashMap<String, Object>(), connection);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (reportBytes != null) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(reportBytes);
}
ResponseBuilder restResponse = Response.ok();
restResponse.header("Content-Disposition",
"attachment; filename=firstSQLReport.pdf");
return restResponse.build();
}
代碼運行正常,我在瀏覽器上收到下載提示 。 但是,當我深入研究Jasper API時,我發現了一個方法runReportToPdfStream()方法,它將為我處理輸出流。
新代碼看起來像這樣:
@GET
@Path("/basicdbreport")
@Produces("application/pdf")
public Response basicDbReport(@Context ServletContext context,
@Context HttpServletResponse response) throws JRException,
IOException {
ServletOutputStream outputStream = response.getOutputStream();
Connection connection = null;
String reportName = "firstsqlexample";
File file = new File(path + reportName + JASPER_EXTENSION);
// check if compiled report exists
if (!file.exists()) {
compileReport(context.getRealPath(path + reportName + JRXML_EXTENSTION));
}
// input steam to fill complied report
InputStream compiledReportStream = context.getResourceAsStream(path
+ reportName + JASPER_EXTENSION);
try {
connection = dataSource.getConnection();
JasperRunManager.runReportToPdfStream(compiledReportStream, outputStream, new HashMap<String, Object>(), connection);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ResponseBuilder restResponse = Response.ok();
restResponse.header("Content-Disposition",
"attachment; filename=firstSQLReport.pdf");
return restResponse.build();
}
代碼運行正常,但我沒有得到任何下載提示 ,pdf在瀏覽器上呈現。 響應標頭如下(在瀏覽器上):
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Wed, 21 Aug 2013 05:51:42 GMT
代碼現在無法提供下載提示的原因是什么? 我不是HTTP的王牌,但我猜這個:
restResponse.header("Content-Disposition",
"attachment; filename=firstSQLReport.pdf");
負責下載選項。 雖然我把它包含在代碼中,但它在響應中沒有任何地方。 請指教。
是的,您是對的, Content-Disposition
是您需要設置為在客戶端瀏覽器上觸發下載操作的響應標頭。
我認為你需要在寫入響應輸出流之前先設置響應頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.