有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将jasper报告生成的PDF文件保存在一个项目文件夹中

我有一个jasper报告,用户已经可以下载了。 但现在需要将此PDF的副本保存到服务器上的特定文件夹中,我有以下代码,但它会生成一个错误

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

我已经试着改变道路了。但我也犯了同样的错误

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

String exportPath = projectPath + "reports/saida";

我的项目文件夹:

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    尝试创建一个目录并获取该目录的绝对路径:

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