jsf - Unable to download the created zipped file correctly from server -
i having folder, trying zip , on button click event should downloaded on user's machine. able generate zip file correctly. have written code download after getting downloaded user's machine server. shows unable open zip file invalid.
how caused , how can solve it? here's code performs download functionality.
public string getaszip() { try { facescontext ctx = facescontext.getcurrentinstance(); externalcontext etx = ctx.getexternalcontext(); httpservletresponse response = (httpservletresponse) etx .getresponse(); servletoutputstream zipfileoutputstream = response .getoutputstream(); response.setcontenttype("application/octet-stream"); response.setheader( "content-disposition", "attachment; filename=" + downloadlink.substring(downloadlink.lastindexof("\\") + 1,downloadlink.length())); response.setheader("cache-control", "no-cache"); file zipfile = new file(downloadlink); fileinputstream stream = new fileinputstream(zipfile); response.setcontentlength(stream.available()); int length = 0; byte[] bbuf = new byte[response.getbuffersize()]; bufferedinputstream in = new bufferedinputstream(stream); bytearrayoutputstream baos = new bytearrayoutputstream(); while ((length = in.read(bbuf)) > 0) { baos.write(bbuf, 0, length); } zipfileoutputstream.write(baos.tobytearray()); zipfileoutputstream.flush(); zipfileoutputstream.close(); response.flushbuffer(); in.close(); stream.close(); } catch (ioexception e) { e.printstacktrace(); } return "successzip"; }
see jsf 2.0 renderresponse , responsecomplete
the problem not call facescontext#responsecomplete(). that's why jsf still render view after attached download , append response. cause zipfile broken.
Comments
Post a Comment