2005 IR Help Posted August 23, 2006 Share Posted August 23, 2006 By: David Lim - aberrant80 Byte arrays and In/Out Stream usage help 2004-03-11 02:40 I don't know very much about in/out streams so I don't really understand how to use these data types with JasperReports. I would appreciate it if someone could enlighten me on their usage and maybe give a quick example code. For one, I'm wondering if I could actually do the file writing within my own code, rather than relying on Jasper's various managers to do the writing for me. How can I actually make use of the btye arrays returned by JasperRunManager, or the output streams in the arguments to help me save to a local file? Thanks in advance. Any guidance appreciated. By: Chuck Deal - cdeal RE: Byte arrays and In/Out Stream usage help 2004-03-11 08:14 Take a look at the managers that exist. The *Manager classes are designed to help out developer's by doing the common things. They don't work in ALL cases, but they do work for most. Particularly, in your case look at the Export Manager and you might get an idea of how to manage the outputstream used by the exporter. By: Carlos Costa e Silva - carloscs RE: Byte arrays and In/Out Stream usage help 2004-03-11 10:21 Here is an example of using in/out streams: final String jasperFile = "Report.jasper"; final URL inUrl = ClassLoader.getSystemResource(jasperFile); if (inUrl == null) { throw new IOException("File Report.jasper not found."); } final File pdfFile = new File("Report.pdf"); InputStream in = null; OutputStream out = null; try { in = inUrl.openStream(); out = new BufferedOutputStream(new FileOutputStream(pdfFile)); JasperRunManager.runReportToPdfStream(in, out, parameters, connection.getDatabaseConnection()); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } For managing yourself the output, use byte[] byteArray = JasperManager.whateverToByteArray(...) and every outptut stream has write(byte[] b) and write(byte[] b, int off, int len) methods. By: MonkeyJoe - monkeyjoe RE: Byte arrays and In/Out Stream usage help 2004-03-11 11:40 Or this if you want to generate pdf in a web app ---------------------------------------------- ServletContext context = request.getSession().getServletContext(); File reportFile = new File(context.getRealPath("report/student_audit_report.jasper")); try { byte[] bytes = JasperRunManager.runReportToPdf( reportFile.getPath(), parameters, new JREmptyDataSource()); response.setContentType("application/pdf"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); } catch (JRException e) { e.printStackTrace(); //To change body of catch statement use Options | File Templates. } enjoy! m By: David Lim - aberrant80 RE: Byte arrays and In/Out Stream usage help 2004-03-11 17:54 Hmmm... I'll try them out. Thanks alot! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now