2015-04-21 69 views
0

我在調用一個索引頁面,該頁面向在PDF中運行碧玉報告的頁面發送參數。但是當我運行生成PDF得到了問題的項目說PDF格式可能不正確或損壞(Mozilla和IE11)使用參數調用jsp頁面的jasper報告

「的index.jsp」

<form method="POST" action="ShowPdf.jsp"> 
    <input type="text" value="" id ="personId" name="personId"> 
    <input type="submit" value="show"> 
</form> 


int id=Integer.parseInt(request.getParameter("personId"));    
try 
{ 
    Connection conn=null; 
    //Connecting to the MySQL database 

    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    conn = DriverManager.getConnection("jdbc:mysql:/localhost:3306/bd_hospital_nikdu", "root", "Admin123"); 

    File reportFile = new File(application.getRealPath("//reports//newReport2.jasper"));//your report_name.jasper file 
    Map parameters = new HashMap(); 
    parameters.put("personId",id); 

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

    //byte[] bytes = JasperRunManager.runReportToHtmlFile(reportFile.getPath(),null, conn).getBytes(); 

    response.setContentType("application/pdf"); 
    response.setContentLength(bytes.length); 

    ServletOutputStream outStream = response.getOutputStream(); 
    outStream.write(bytes, 0, bytes.length); 

    outStream.flush(); 
    outStream.close(); 

} 
catch (Exception ex) { 
    ex.printStackTrace(); 
} 
+1

您好,歡迎來到Stack Overflow。請閱讀http://stackoverflow.com/help/how-to-ask和http://stackoverflow.com/help/mcve,並嘗試改進您的問題以包含所有相關信息,以便我們爲您提供幫助。因爲像現在這樣,沒有任何代碼很難推斷出你的問題。 –

+0

請將代碼放在此處,以便其他人可以分析您的問題! –

+0

我已經使用code..plzz編輯我的問題 –

回答

0

這裏是我的解決方案

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

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

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

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

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

<%@ page import="net.sf.jasperreports.view.JRViewer" %> 

<%@ page import="com.itextpdf.text.*"%> 

<%@ page import="com.itextpdf.text.pdf.*"%> 

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

<% 

      /*Parametros para realizar la conexión*/ 

      Connection conexion; 

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

      conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/gae", "root", ""); 

      /*Establecemos la ruta del reporte*/ 

      File reportFile = new File(application.getRealPath("reportes//prueba1.jasper")); 

      /* No enviamos parámetros porque nuestro reporte no los necesita asi que escriba cualquier cadena de texto ya que solo seguiremos el formato del método runReportToPdf*/ 

      Map parameters = new HashMap(); 

      parameters.put("cedula", "cedula"); 

      parameters.put("nombre", "nombre"); 

      /*Enviamos la ruta del reporte, los parámetros y la conexión(objeto Connection)*/ 

      byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conexion); 

      /*Indicamos que la respuesta va a ser en formato PDF*/ 

      response.setContentType("application/pdf"); 

      response.setContentLength(bytes.length); 

      ServletOutputStream ouputStream = response.getOutputStream(); 

      ouputStream.write(bytes, 0, bytes.length); /*Limpiamos y cerramos flujos de salida*/ 

      ouputStream.flush(); 

      ouputStream.close(); 

%> 
<html> 

    <head> 

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

     <title>JSP Page</title> 

    </head> 

    <body> 

     <h1>Hello World!</h1> 

    </body> 

</html>