1 28 package net.sf.jasperreports.j2ee.servlets; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.util.List ; 34 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletOutputStream ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import net.sf.jasperreports.engine.JRException; 41 import net.sf.jasperreports.engine.JRExporterParameter; 42 import net.sf.jasperreports.engine.export.JRRtfExporter; 43 44 48 public class RtfServlet extends BaseHttpServlet 49 { 50 51 52 55 public void service( 56 HttpServletRequest request, 57 HttpServletResponse response 58 ) throws IOException , ServletException 59 { 60 List jasperPrintList = BaseHttpServlet.getJasperPrintList(request); 61 62 if (jasperPrintList == null) 63 { 64 throw new ServletException ("No JasperPrint documents found on the HTTP session."); 65 } 66 67 Boolean isBuffered = Boolean.valueOf(request.getParameter(BaseHttpServlet.BUFFERED_OUTPUT_REQUEST_PARAMETER)); 68 if (isBuffered.booleanValue()) 69 { 70 JRRtfExporter exporter = new JRRtfExporter(); 71 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); 72 73 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 74 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 75 76 try 77 { 78 exporter.exportReport(); 79 } 80 catch (JRException e) 81 { 82 throw new ServletException (e); 83 } 84 85 byte[] bytes = baos.toByteArray(); 86 87 if (bytes != null && bytes.length > 0) 88 { 89 response.setContentType("application/rtf"); 90 response.setHeader("Content-Disposition", "inline; filename=\"file.rtf\""); 91 response.setContentLength(bytes.length); 92 ServletOutputStream ouputStream = response.getOutputStream(); 93 94 try 95 { 96 ouputStream.write(bytes, 0, bytes.length); 97 ouputStream.flush(); 98 } 99 finally 100 { 101 if (ouputStream != null) 102 { 103 try 104 { 105 ouputStream.close(); 106 } 107 catch (IOException ex) 108 { 109 } 110 } 111 } 112 } 113 } 124 else 125 { 126 response.setContentType("application/rtf"); 127 response.setHeader("Content-Disposition", "inline; filename=\"file.rtf\""); 128 129 JRRtfExporter exporter = new JRRtfExporter(); 130 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); 131 132 OutputStream ouputStream = response.getOutputStream(); 133 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 134 135 try 136 { 137 exporter.exportReport(); 138 } 139 catch (JRException e) 140 { 141 throw new ServletException (e); 142 } 143 finally 144 { 145 if (ouputStream != null) 146 { 147 try 148 { 149 ouputStream.close(); 150 } 151 catch (IOException ex) 152 { 153 } 154 } 155 } 156 } 157 } 158 159 160 } 161 162 | Popular Tags |