java - JSF CSV download in bean -
java - JSF CSV download in bean -
i trying csv download in same fashion here: how stream file download in jsf backing bean?
my response keeps throwing nullpointerexception @ output.write() line. bean of request scope. thoughts null pointer?
seek { //submitform(); facescontext fc = facescontext.getcurrentinstance(); httpservletresponse response = (httpservletresponse) fc.getexternalcontext().getresponse(); response.reset(); response.setcontenttype("text/csv"); //response.setcontentlength(contentlength); response.setheader ( "content-disposition", "attachment; filename=\"reporting-" + new date().gettime() + ".csv\"" ); outputstream output = response.getoutputstream(); string s = "\"project #\",\"project name\",\"product feature(s)\","; s+="\"project status\","; s+="\"install type\","; s+="\"beta test\",\"beta test new/updated\","; s+="\"production\",\"production new/updated\","; s+="\n"; inputstream = new bytearrayinputstream( s.getbytes("utf-8") ); int nextchar; while ((nextchar = is.read()) != -1) { output.write(nextchar); } output.close(); } grab ( ioexception e ) { // todo auto-generated grab block e.printstacktrace(); }
3 things jump out here
failing phone call responsecomplete() on facescontext pretty much means jsf go on process request , don't impact outcome.
the reset() phone call unnecessary.
the outputstream should of type servletoutputstream
try next snippet instead
try { //submitform(); facescontext fc = facescontext.getcurrentinstance(); httpservletresponse response = (httpservletresponse) fc.getexternalcontext().getresponse(); response.setcontenttype("text/csv"); fc.responsecomplete(); //response.setcontentlength(contentlength); response.setheader ( "content-disposition", "attachment; filename=\"reporting-" + new date().gettime() + ".csv\"" ); servletoutputstream output = response.getoutputstream(); string s = "\"project #\",\"project name\",\"product feature(s)\","; s+="\"project status\","; s+="\"install type\","; s+="\"beta test\",\"beta test new/updated\","; s+="\"production\",\"production new/updated\","; s+="\n"; inputstream = new bytearrayinputstream( s.getbytes("utf-8") ); int nextchar; while ((nextchar = is.read()) != -1) { output.write(nextchar); } output.flush(); output.close(); } grab ( ioexception e ) { // todo auto-generated grab block e.printstacktrace(); } additionally, can phone call sos.println(s) without need work you're doing there
java jsf csv download
Comments
Post a Comment