如何使用JasperReports API将生成的pdf保存到服务器 [英] How to save generated pdf with JasperReports API into server

查看:68
本文介绍了如何使用JasperReports API将生成的pdf保存到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将生成的 PDF 文件保存到服务器中.我正在使用 JasperReports API .

I need to save my generated PDF file into my server. I am using JasperReports API.

用于生成 PDF 的代码示例:

//Result set(rs)
//Report path (rptPath)
//Hash map (hmp)
//ServletOutputStream (sos)
//HttpServletResponse (resp)

JRResultSetDataSource jrrs = new JRResultSetDataSource(rs);
bytes = JasperRunManager.runReportToPdf(rptPath, hmp, jrrs);
sos = resp.getOutputStream();
resp.setContentType("application/pdf");

resp.setHeader("Content-Disposition", "attachment;filename="MyFile.pdf");

sos.write(bytes);

sos.flush();
sos.close();

它直接生成文件并要求下载.我要将生成的文件存储到服务器的位置.

It directly generates the file and ask for download. Where I want to store the generated file into server.

推荐答案

您需要将字节写入服务器上的本地文件,而不是为此将其写回到HttpResponse. 您的代码如下所示:

You need to write bytes to the local file on server instead of writing it back to HttpResponse for that. Your code can look like :

FileOutputStream fileOuputStream = new FileOutputStream("C:\\report.pdf");
fileOuputStream.write(bytes);
fileOuputStream.close();

这篇关于如何使用JasperReports API将生成的pdf保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆