简体   繁体   中英

Sending ZIP file from servlet to client not working

I am trying to send a zip file from a Java servlet to a JavaScript client. I first generate the zip archive on the disk, and then I try to send it to the client. The zip generation works fine. However, I have a problem with sending it to the client.

The code related to sending the file to the client (inside doPost ) is the 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 is = new FileInputStream(f);     
  is.read(arBytes);     
  ServletOutputStream op = response.getOutputStream();     
  op.write(arBytes);     
  op.flush();    

This is the Ajax request from the 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( this )
} );

In the browser console I get the following 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 I go to the Source tab, then I get Failed to load resource after the last line of the following 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 I solve the problem?

You need to check how many bytes have been written using the return code FileInputStream.read() . Try something like 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 is = 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 from the question. It is just wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM