java - Sending ZIP file from servlet to client not working -
i trying send zip file java servlet javascript client. first generate zip archive on disk, , seek send client. zip generation works fine. however, have problem sending client.
the code related sending file client (inside dopost) following:
string path = getservletcontext().getrealpath("/") + "generatedapps/"; string filename = appname + "archive.zip"; file f = new file(path + filename); response.setcontenttype("application/zip"); response.setcontentlength((int)f.length()); response.addheader("content-encoding", "gzip"); response.addheader("content-disposition","attachment;filename=\"" + filename + "\""); byte[] arbytes = new byte[(int)f.length()]; fileinputstream = new fileinputstream(f); is.read(arbytes); servletoutputstream op = response.getoutputstream(); op.write(arbytes); op.flush(); this ajax request client:
new ajax.request( source, { asynchronous: false, method: 'post', parameters: {content: rwsmlfile}, onsuccess: function(transport){ console.log(transport); }.bind(this), onfailure: (function ( transport ) { oryx.log.error( "sending rwsml file failed! info: " + transport ); }).bind( ) } ); in browser console next error:
post http://localhost:8080/oryx/generategeddyjscode prototype-1.5.1.js:1044 ajax.request.object.extend.request prototype-1.5.1.js:1044 ajax.request.object.extend.initialize prototype-1.5.1.js:1006 (anonymous function) prototype-1.5.1.js:37 oryx.plugins.rwsmlsupport.oryx.plugins.abstractplugin.extend.generategeddyjscode rwsmlsupport.js:47 (anonymous function) prototype-1.5.1.js:105 a.each.j.functionality default.js:2828 ext.button.ext.extend.onclick ext-all.js:87 v ext-all.js:13 o if go source tab, failed load resource after lastly line of next code snippet:
if (this.options.oncreate) this.options.oncreate(this.transport); ajax.responders.dispatch('oncreate', this, this.transport); this.transport.open(this.method.touppercase(), this.url, this.options.asynchronous); if (this.options.asynchronous) settimeout(function() { this.respondtoreadystate(1) }.bind(this), 10); this.transport.onreadystatechange = this.onstatechange.bind(this); this.setrequestheaders(); this.body = this.method == 'post' ? (this.options.postbody || params) : null; this.transport.send(this.body); how can solve problem?
you need check how many bytes have been written using homecoming code fileinputstream.read(). seek this:
string path = getservletcontext().getrealpath("/") + "generatedapps/"; string filename = appname + "archive.zip"; file f = new file(path + filename); response.setcontenttype("application/zip"); response.setcontentlength((int)f.length()); response.addheader("content-disposition","attachment;filename=\"" + filename + "\""); byte[] arbytes = new byte[32768]; fileinputstream = new fileinputstream(f); servletoutputstream op = response.getoutputstream(); int count; while ((count = is.read(arbytes)) > 0) { op.write(arbytes, 0, count); } op.flush(); edit:
removed response.addheader("content-encoding", "gzip"); copied question. wrong.
java javascript ajax servlets client
No comments:
Post a Comment