是否可以将 jasper report 与 java 一起使用,使其像浏览器窗口中的弹出窗口一样?

我正在使用 Jasper 报告和 java。我想让导出文件在作为网络应用程序启动时询问下载位置。


我现在正在做的是。我给出了要在某个位置导出的文件(PDF、DOCX、XLS)的路径。我需要的是,浏览器应该像 Firefox 一样弹出一个窗口并要求下载文件位置。


我已经在java应用程序中通过提供路径完成了它,


Connection conn = null;

    ResultSet rs = null;

    JasperReport jasperReport = null;

    JasperPrint print = null;

    String filename = "Report.pdf";

    String query = "{CALL get_report_data()}";

    try {

        if (conn == null) 

        {

            String hostName = "localhost";

            String dbName = "test";

            String userName = "root";

            String password = "root";

            try {

                Class.forName("com.mysql.cj.jdbc.Driver");

                String connectionURL = "jdbc:mysql://" + hostName + ":3306/" + dbName;

                conn = DriverManager.getConnection(connectionURL, userName, password);

            } catch (Exception e) {

                System.out.println(e);

            }

        }

        CallableStatement cstmt = conn.prepareCall(query);


        rs = cstmt.executeQuery();


        JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);


        jasperReport = JasperCompileManager

                .compileReport("E:\\Eclipse 2019-03 Workspace\\Report\\static_land_report.jrxml");


        Map<String, Object> parameters = new HashMap<String, Object>();


        print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);

JasperExportManager.exportReportToPdfFile(print, "E:\Eclipse 2019-03 Workspace\Report\static_land_report.pdf");


现在,我希望上面的代码在网络应用程序中完成,并且不提供路径。


宝慕林4294392
浏览 108回答 2
2回答

四季花海

按照以下步骤尝试一下。转到 iReport 中的存储库选项卡选择保存输入控件的文件夹右键单击该文件夹,选择添加 -> inputcontrol在 id 字段中,输入您的参数名称(必须与 $P{} 之间的值完全相同)给它一个提示名称在输入控件详细信息选项卡中,选择单个值,然后选择数据类型(您可以在 iReport 中以相同的方式创建数据类型)然后转到报表文件夹,在输入控制图上右键单击并选择链接现有输入控件,然后选择刚刚创建的输入控件。

慕斯709654

但是,我找到了解决方案。package com.report.java;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.ResultSet;import java.util.HashMap;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.jasperreports.engine.JRResultSetDataSource;import net.sf.jasperreports.engine.JasperCompileManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.export.JRPdfExporter;import net.sf.jasperreports.export.SimpleExporterInput;import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;public class PdfReportDownload extends HttpServlet {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; public PdfReportDownload() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; }&nbsp; &nbsp; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; generatePdfReport(response);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; doGet(request, response);&nbsp; &nbsp; }&nbsp; &nbsp; public void generatePdfReport(HttpServletResponse response)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection conn=null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet rs=null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JasperReport jasperReport = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String query ="{CALL get_report_data()}";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(conn==null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn= MySQLConnection.getConnection();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CallableStatement cstmt = conn.prepareCall(query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs = cstmt.executeQuery();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jasperReport = JasperCompileManager.compileReport("E:\\Eclipse 2019-03 Workspace\\Report_Download\\static_land_report.jrxml");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap <String, Object>(), resultSetDataSource);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JRPdfExporter pdfExporter = new JRPdfExporter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream pdfReportStream = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfReportStream));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdfExporter.exportReport();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.setContentType("application/pdf");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.setHeader("Content-Length", String.valueOf(pdfReportStream.size()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.addHeader("Content-Disposition", "attachment; filename=Report.pdf;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputStream responseOutputStream = response.getOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseOutputStream.write(pdfReportStream.toByteArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseOutputStream.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdfReportStream.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}调用此 servlet 时,将下载报告
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java