0

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.

Alex K
  • 22,315
  • 19
  • 108
  • 236
JDGuide
  • 6,239
  • 12
  • 46
  • 64
  • My above code is generating a pdf file.I need to save this file into server.It should not ask for download the file. – JDGuide Aug 29 '12 at 13:13
  • So write the file to disk using Java IO capabilities... I do not really understand your problem. – home Aug 29 '12 at 13:15
  • The generated file should not ask for download.It should saved internally into my server. – JDGuide Aug 29 '12 at 13:17

1 Answers1

1

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();
Saurabh
  • 7,894
  • 2
  • 23
  • 31
  • Thanks mate.But, you are writing the file here.But, I need to store that same file into server. No need to ask for download, internally i need to save that file. – JDGuide Aug 29 '12 at 13:22
  • Does not this code get executed on server itself ? If yes then the file write will happen local that is server itself right ? – Saurabh Aug 29 '12 at 13:23
  • Yes you are right , but, is there any other way to save that file directly into server ? – JDGuide Aug 29 '12 at 13:25
  • 1
    You can may be use runReportToPdfFile(http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperRunManager.html#runToPdfFile(java.lang.String, java.util.Map)) API http://www.ensode.net/jasperreports_database_pg3.html – Saurabh Aug 29 '12 at 13:35