1

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

Alex K
  • 22,315
  • 19
  • 108
  • 236
sergioBertolazzo
  • 585
  • 2
  • 11
  • 27
  • 2
    This you trying to do doesn't work beacause this folder is not accessible. One way to achieve what you want is write the pdf bytearray or stream object into the response. something like this https://stackoverflow.com/a/3592258/3038067. – Diogo Calazans Jul 19 '17 at 19:21
  • Thanks for your awnser, but i already can export the pdf to brownser, now i need save a copy in the server. Have anyway to make this folder accessible ? – sergioBertolazzo Jul 19 '17 at 19:44
  • Try and syso the file path i..e exportPath variable.. may be the path is not accessible. Else nothing seems wrong. – s s Jul 20 '17 at 11:17

1 Answers1

5

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");
20comer
  • 161
  • 2
  • 1
    Is there any way to save the output file in the server file system instead of saving it in project path.? – MaYur MahAjan Nov 22 '18 at 12:30
  • @MaYurMahAjan Yes, if your server is Windows you can use something like File file = new File("C:/temp/output") If your server is Linux you can use something like File file = new File("/opt/output") – 20comer Nov 28 '18 at 10:12