将jasper报告下载到服务器中的特定位置

时间:2015-06-17 09:02:28

标签: java servlets jasper-reports

我的应用程序在tomcat服务器上运行,我已经开发了一种使用jasper下载pdf文件的方法。我需要做的是,将某些pdf下载到服务器内的某个位置。我试过了,我不知道怎么做。

            jasperReport = JasperCompileManager.compileReport(absolutePathToIndexJSP);
            JasperPrint gh = JasperFillManager.fillReport(jasperReport,parameters, JRdataSource);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            Exporter exporter = new Exporter();
            String downloadFileName = "";


            downloadFileName = "Fund-Transfer-Report-" + reportGenerateDate + ".pdf";
                response.setHeader("Content-Disposition", "attachment; filename=" + downloadFileName);
                response.setContentType("application/pdf");
                exporter.exportPdf(response, gh, baos);

这将转到下面的导出方法,

public void exportPdf(HttpServletResponse response, JasperPrint jp, ByteArrayOutputStream baos) throws JRException, IOException {

    //OutputStream ouputStream = new FileOutputStream(new File("C:/JasperReports/Account_Report.pdf"));
    //Get response output stream to download report file
    OutputStream ouputStream = response.getOutputStream();

    // Create a JRPdfExporter instance
    JRPdfExporter pdfExporter = new JRPdfExporter();

    // Here we assign the parameters jp and baos to the exporter
    pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
    pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

    pdfExporter.exportReport();

    ouputStream.write(baos.toByteArray());
    ouputStream.flush();
    ouputStream.close();
  }

这样做是将PDF正常下载到下载文件夹。我需要能够将其下载到服务器中的某个位置。请指教。

2 个答案:

答案 0 :(得分:1)

您可以使用JasperExportManager将生成的pdf保存到服务器中的特定位置

JasperExportManager.exportReportToPdfFile(jasperPrint, exportPath);

例如:

String exportDir = System.getProperty("jboss.server.config.dir") +"/jasperreport/export";
String exportPath = exportDir + "/reportName.pdf";
JasperReport jasperReport = CompileManager.compileReport(jrxmlFilePath, mainReportName);
JasperPrint jasperPrint =JasperFillManager.fillReport(jasperReport, parameters, jsonDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, exportPath);

答案 1 :(得分:1)

我已经编码了这样,并且在给定文件夹路径的本地(服务器)机器上正常工作:

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Connection conn=null;
    JasperPrint jp=null;

    // month and year...
    int month=8;
    int year=2015;

    try{
        // DB connection...
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","sachin");
        HashMap<String, Object> hm=new HashMap<String, Object>();

         //    .jrxml file path (location)
        String path="/home/sachin/sachin/workspace/JasperReport1/WebContent/report/SalaryReport12.jrxml";


        JasperReport design=JasperCompileManager.compileReport(path);
        jp=JasperFillManager.fillReport(design,null,conn);

        // folder creation path configured in web.xml
         String basePath=(String) request.getSession().getServletContext().getInitParameter("folderpath");
       // name for folder as month_year
        basePath=basePath+"/"+month+"_"+year;
        // name for pdf file and saves in a created folder
        String exportPath = basePath + "/salarySlip.pdf";
        String jasperReport = JasperCompileManager.compileReportToFile(path); 
        JasperPrint jasperPrint =JasperFillManager.fillReport(jasperReport, hm,conn);
        JasperExportManager.exportReportToPdfFile(jasperPrint, exportPath);


    }catch(Exception e){

        System.err.println(e);
    }

 }