Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从servlet向客户端发送ZIP文件不起作用_Java_Javascript_Ajax_Servlets_Client - Fatal编程技术网

Java 从servlet向客户端发送ZIP文件不起作用

Java 从servlet向客户端发送ZIP文件不起作用,java,javascript,ajax,servlets,client,Java,Javascript,Ajax,Servlets,Client,我正在尝试将一个zip文件从Javaservlet发送到JavaScript客户端。我首先在磁盘上生成zip存档,然后尝试将其发送到客户端。zip一代工作正常。但是,我在将其发送到客户端时遇到问题 与将文件发送到客户端(在doPost中)相关的代码如下: String path = getServletContext().getRealPath("/") + "generatedApps/"; String fileName = appName + "Archive.zip";

我正在尝试将一个zip文件从Javaservlet发送到JavaScript客户端。我首先在磁盘上生成zip存档,然后尝试将其发送到客户端。zip一代工作正常。但是,我在将其发送到客户端时遇到问题

与将文件发送到客户端(在
doPost
中)相关的代码如下:

  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();    
这是来自客户端的Ajax请求:

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 )
} );
在浏览器控制台中,我收到以下错误:

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
如果我转到
Source
选项卡,则在以下代码段的最后一行之后会出现
加载资源失败的情况:

  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);

如何解决此问题?

您需要检查使用返回代码
FileInputStream.read()
写入的字节数。试着这样做:

  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();    
编辑:


删除了
response.addHeader(“内容编码”、“gzip”)从问题中复制。这是错误的。

您需要检查使用返回代码
FileInputStream.read()
写入的字节数。试着这样做:

  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();    
编辑:


删除了
response.addHeader(“内容编码”、“gzip”)从问题中复制。这是错误的。

Ahh还删除了这一行:
response.addHeader(“内容编码”,“gzip”)好的,我收到一些东西<代码>主键���D(MyApp/app/Controller/ObjectTypeNames[…]
…我想这个奇怪的字符串是因为一个zip文件不能显示在
响应
字段中…我说得对吗?…你也知道如何下载收到的文件吗?我想完成的实际任务是下载一个由servlet生成的zip文件,尽管我知道这不能用jax,我想也许有一种简单的方法可以根据servlet发送的结果在客户机中构造文件……最后我放弃了Ajax调用,并用一个表单替换了它……这很有效,我可以下载我的文件……感谢您帮助我发送文件……ahh还删除了这一行:
response.addHeader(“Content Encoding”,“gzip”)
好的,我收到了一些东西……
PK���D(MyApp/app/Controller/ObjectTypeNames[…]
…我想这个奇怪的字符串是因为一个zip文件不能显示在
响应
字段中…我说得对吗?…你也知道如何下载收到的文件吗?我想完成的实际任务是下载一个由servlet生成的zip文件,尽管我知道这不能用jax,我想也许有一种简单的方法可以根据servlet发送的结果在客户机中构造文件…最后我放弃了Ajax调用,用一个表单替换了它…这很有效,我可以下载我的文件…感谢您帮助我发送文件