Search code examples
javajasper-reports

How to save a PDF file generated with jasper reports in one of the project folders


I have a jasper report that is already working for the user to download. But now there is a need to save a copy of this PDF to a specific folder on the server, I have the following code, but it generates an error.

            JasperDesign design = JRXmlLoader.load(jrxPath);
            JasperReport report = JasperCompileManager.compileReport(design);                
            JasperPrint print = JasperFillManager.fillReport(report, getParametros());           

            //String pojectPath = "http://localhost:9080/ProjectName/";
            String exportPath = projectPath + "reports/saida/" + "reportName.pdf";
            JasperExportManager.exportReportToPdfFile(print, exportPath);

net.sf.jasperreports.engine.JRRuntimeException: java.io.FileNotFoundException: http:\localhost:9080\RelatoriosDPOC\reports\saida\reportName.pdf
(The syntax for the file name, directory name, or volume label is incorrect.)

I already tried to change the path out. But I get the same error.

 String exportPath = projectPath + "reports/saida/";

and

String exportPath = projectPath + "reports/saida";

My project folders:

enter image description here


Solution

  • Try to create a dir and get the absolut path of this dir:

      String realPath = getServletContext().getRealPath("/");
    
      File file = new File(realPath+"/reports/saida/output/");
      file.mkdirs();
    
      JasperExportManager.exportReportToPdfFile(print, file.getAbsolutePath +"reportName.pdf");