JDGuide
JDGuide

Reputation: 6525

How to save generated pdf with JasperReports API into server

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

Code sample for PDF generation:

//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.

Upvotes: 0

Views: 6148

Answers (1)

Saurabh
Saurabh

Reputation: 7964

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();

Upvotes: 1

Related Questions