将jasperreport作为HTML发送

时间:2013-03-14 09:13:51

标签: java jsp servlets jasper-reports

我使用JapserReports在Java中显示报告。我可以将PDF格式的报告发送到网络浏览器。

现在我想以HTML格式发送报告,以下是我以HTML格式发送报告的方法。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    File reportFile = new File(getServletConfig().getServletContext().getRealPath("/rpts/report1.jasper"));
    ServletOutputStream servletOutputStream = response.getOutputStream();
    byte[] bytes = null;
    Map<String,Object> parameter = new HashMap<String,Object>();
    Connection con = DBConnection.getConnection("");
    try
    {
        bytes = JasperRunManager.runReportToHtmlFile(reportFile.getPath(),parameter,con).getBytes();
        response.setContentType("text/html");
        response.setContentLength(bytes.length);
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();
    }
    catch (JRException e)
    {
        System.out.println(e);
    }
}

上面的方法是打印生成的path文件的HTML。当我检查路径时,那里生成了HTML文件和数据。

所以我在代码中遗漏了什么?

提前致谢....

2 个答案:

答案 0 :(得分:3)

尝试设置Content-Disposition和html文件名。

    response.setHeader("Content-Disposition","inline, filename=myReport.html");
    response.setContentType("text/html");
    response.setContentLength(bytes.length);
    servletOutputStream.write(bytes, 0, bytes.length);
    servletOutputStream.flush();
    servletOutputStream.close();

runReportToHtmlFile方法返回绝对文件路径。在写入输出流之前,需要将生成的html文件读入字节数组。

下面的代码可能适合您。

        String reportPath =JasperRunManager.runReportToHtmlFile(reportFile.getPath(), parameter);
        File reportHtmlFile = new File(reportPath);
        FileInputStream fis = new FileInputStream(reportHtmlFile);
        byte[] bytes =  new byte[(int)reportHtmlFile.length()];
        fis.read(bytes);
        resp.setHeader("Content-Disposition","inline; filename=myReport.html");
        resp.setContentType("text/html");
        resp.setContentLength(bytes.length);
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();

答案 1 :(得分:-1)

试试这个

public class ReportExample {



/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList<City> cityList=new ArrayList<City>();

City city=new City();

city.setCityName("Kolkata");

city.setCountry("Country");

cityList.add(city);

InputStream inputStream;

try {

inputStream = new FileInputStream ("report1.jrxml");

DataBeanMaker dataBeanMaker = new DataBeanMaker();

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(cityList);

Map parameters = new HashMap();



JasperDesign jasperDesign = JRXmlLoader.load(inputStream);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

JasperExportManager.exportReportToPdfFile(jasperPrint, "testjasper.pdf"); 

   JasperViewer.viewReport(jasperPrint);

   JasperPrintManager.printReport(jasperPrint, true);

}catch(Exception e)

{e.printStackTrace();

}

}



}

 check the link http://javadispute.com/content/jasper-report-example