简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM