Spring mvc jasper报告使用jsp转换为pdf时出错。显示无法加载pdf

Spring mvc jasper报告使用jsp转换为pdf时出错。显示无法加载pdf,spring-mvc,jasper-reports,pdf-generation,Spring Mvc,Jasper Reports,Pdf Generation,Search.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> &

Search.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JSP Page</title>
</head>

<body>

   <h3>Search the id which you want</h3>

   <form method="POST" action="filereport.jsp">

       <p>Give the searching ID</p>

       <input type="text" name="employid"/><br>

       <input type="submit" value="show"/>

   </form>

 </body>

</html>
<%@page import="java.sql.SQLException"%>

<%@ page  import="java.io.*"%> 

<%@ page  import="java.sql.Connection"%> 

<%@ page  import="java.sql.DriverManager"%>

<%@ page  import="java.util.HashMap"%>

<%@ page  import="java.util.Map"%>

<%@ page  import="net.sf.jasperreports.engine.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="Class.*" %>

<!DOCTYPE HTML>

<html>

   <head>

       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

       <title>JSP Page</title>

   </head>

   <body>

       <h2>your searching result is :</h2>

       <%

          Connection conn=null;

          try{

              //connection to mysql database

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

           conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product?user=root&password=mysql123");

         ////Loading Jasper Report File from Local file system

           String jrxmlFile = session.getServletContext().getRealPath(request.getContextPath())+"/Reports/reportgenerate.jasper";

           InputStream input = new FileInputStream(new File(jrxmlFile));

           Map parameters = new HashMap();

           int id=Integer.parseInt(request.getParameter("employid"));

           parameters.put("employeeid", id);

           //generating the report
           JasperReport jasperReport = JasperCompileManager.compileReport(input);

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

           //byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn.getconnection());

           //exporting the report as pdf
//          
           ////Exporting the report as a PDF 

           JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

          }catch(FileNotFoundException e){

              e.printStackTrace();

          }catch(JRException e){

              e.printStackTrace();

          }catch(ClassNotFoundException e){

              e.printStackTrace();

          }catch(SQLException e){

              e.printStackTrace();

          }finally{

              if(conn!=null){

                  conn.close();
              }
          }

       %>

   </body>

</html>

JSP页面
搜索您想要的id
给出搜索ID


filereport.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JSP Page</title>
</head>

<body>

   <h3>Search the id which you want</h3>

   <form method="POST" action="filereport.jsp">

       <p>Give the searching ID</p>

       <input type="text" name="employid"/><br>

       <input type="submit" value="show"/>

   </form>

 </body>

</html>
<%@page import="java.sql.SQLException"%>

<%@ page  import="java.io.*"%> 

<%@ page  import="java.sql.Connection"%> 

<%@ page  import="java.sql.DriverManager"%>

<%@ page  import="java.util.HashMap"%>

<%@ page  import="java.util.Map"%>

<%@ page  import="net.sf.jasperreports.engine.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="Class.*" %>

<!DOCTYPE HTML>

<html>

   <head>

       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

       <title>JSP Page</title>

   </head>

   <body>

       <h2>your searching result is :</h2>

       <%

          Connection conn=null;

          try{

              //connection to mysql database

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

           conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product?user=root&password=mysql123");

         ////Loading Jasper Report File from Local file system

           String jrxmlFile = session.getServletContext().getRealPath(request.getContextPath())+"/Reports/reportgenerate.jasper";

           InputStream input = new FileInputStream(new File(jrxmlFile));

           Map parameters = new HashMap();

           int id=Integer.parseInt(request.getParameter("employid"));

           parameters.put("employeeid", id);

           //generating the report
           JasperReport jasperReport = JasperCompileManager.compileReport(input);

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

           //byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn.getconnection());

           //exporting the report as pdf
//          
           ////Exporting the report as a PDF 

           JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

          }catch(FileNotFoundException e){

              e.printStackTrace();

          }catch(JRException e){

              e.printStackTrace();

          }catch(ClassNotFoundException e){

              e.printStackTrace();

          }catch(SQLException e){

              e.printStackTrace();

          }finally{

              if(conn!=null){

                  conn.close();
              }
          }

       %>

   </body>

</html>

JSP页面
您的搜索结果是:
问题是当我运行文件时,会打开一个空白的pdf视图,并显示一个错误“无法加载pdf文档”。有人能帮忙吗

提前感谢。

我可以看到两个问题:

  • 您应该
    刷新
    关闭
    response.getOutputStream()
  • 范例

    JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    response.getOutputStream().flush();
    response.getOutputStream().close();
    
    考虑对
    close()
    使用
    try
    catch
    语句,并为响应添加正确的标题

  • 如果报表数据源为空,并且您的
    jasperReport
    标记上没有属性
    whenNodeType
    ,它将生成一个空pdf,这可能会导致问题。当nodatatype=“BlankPage”或任何您认为最合适的设置时,您可以更改此行为设置。看

  • 你还有问题吗?@PetterFriberg-嘿,谢谢,但我尝试了你的解决方案,仍然有问题。不明白还有什么问题!和以前一样的问题还是其他问题?