public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ GetAndPostExample sqlConn = new GetAndPostExample(); String path = "D:\\bems\\workspace\\flex_html\\src\\jspreport\\flexhtml.jasper"; File reportFile1 = new File(path); JasperReport jasperReport = (JasperReport) JRLoader .loadObject(reportFile1.getPath()); System.out.println(reportFile1.getPath()); if (!reportFile1.exists()) throw new JRRuntimeException("没找到合同模板路径");
Connection conn= sqlConn.getConnection(); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, null, conn); JRHtmlExporter exporter = new JRHtmlExporter(); response.setCharacterEncoding("GBK"); //处理中文乱码 PrintWriter writer=response.getWriter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,writer ); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK"); //处理中文乱码 exporter.exportReport(); writer.flush(); conn.close(); }catch (SQLException e1) { e1.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
|