java - Using PrintWriter and OutputStream -


i creating project struts , have problem using jasper ireports. want export info pdf file , keep getting java.lang.illegalstateexception: getoutputstream() has been call... exception due openning servletoutputstream in code when page opens printwriter.

the code in model (so not in jsp, it's in java file), follows:

    public void handle(httpservletresponse res, connection connection, string path)throws exception{     servletoutputstream out = null;     try {          jasperdesign jasperdesign = jrxmlloader.load(path);         jasperreport jasperreport = jaspercompilemanager.compilereport(jasperdesign);         byte[] bytes = jasperrunmanager.runreporttopdf(jasperreport, null, connection);         res.setcontenttype("application/pdf");         res.setcontentlength(bytes.length);         out = res.getoutputstream();         out.write(bytes, 0, bytes.length);     } catch (exception e) {         e.printstacktrace();     } {         out.flush();         out.close();     } 

i have checked connection, path , httpservletresponse , working fine.

i'm newbie jasper reports coding stuff pdf can -correctly- suposse have minimal idea of doing here , that, code copy/pasted somewhere through net.

i have tried use printwriter instead of outputstream, transforming bytes string , using printwriter.append(string) method (allthought not string charsequence), doesn't extract data pdf.

i have tried printwriter, close open outputstream (didn't work) or flush (neither).

any solution use out show data in pdf great. lot!

would useful see stack trace.

you might try running sanity check first though: modify code write static string (hello world) servletoutputstream , set content type text/html. should work fine:

public void handle(httpservletresponse res, connection connection, string path)throws exception{ servletoutputstream out = null; try {     byte[] bytes = "hello world".getbytes();     res.setcontenttype("text/html");     res.setcontentlength(bytes.length);     out = res.getoutputstream();     out.write(bytes, 0, bytes.length); } catch (exception e) {     e.printstacktrace(); } {     out.flush();     out.close(); } 

hth


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -