ajax下载文档 ClientAbortException: java.net.SocketException: Connection reset by pee

众所周知ajax是使用了浏览器内部的XmlHttpRequest对象来传输XML数据的。既然是Xml的数据传输,那么传输的数据肯定是文本的,而文件上传则需要传输二进制的数据,显然用ajax是不可能的。

这里要说的是用ajax上传文件其实只是类似ajax的那种操作,实际上并没有用到xmlhttprequest对象。从网上找到模拟form提交的方式实现此功能,如下所示:

 

    var form = $("<form>");   //定义一个form表单
    form.attr('style','display:none');   //在form表单中添加查询参数
    form.attr('target','');
    form.attr('method','post');
    form.attr('action',_url);
    $('body').append(form);  //将表单放置在web中
    form.submit();   //表单提交

 然而这种方式在火狐和IE8中都能正常运行,只有在IE6下总是报如下错误:

 

ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
at org.apache.catalina.connector.OutputBuffer.realWri teBytes(OutputBuffer.java:366)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(B yteChunk.java:403)
at org.apache.catalina.connector.OutputBuffer.doFlush (OutputBuffer.java:311)
at org.apache.catalina.connector.OutputBuffer.flush(O utputBuffer.java:293)
at org.apache.catalina.connector.Response.flushBuffer (Response.java:534)
at org.apache.catalina.core.StandardHostValve.status( StandardHostValve.java:285)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:743)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

 上网找引起这个错误的原因归纳如下:

①:服务器的并发连接数超过了其承载量,服务器会将其中一些连接Down掉;
②:客户关掉了浏览器,而服务器还在给客户端发送数据;
③:浏览器端按了Stop 

 

而很显然我的原因并不符合这三种,应该是与特定的浏览器解析有关,又有解决方法说用IE下载文档时要设置Cache-Control

 

response.addHeader("Cache-Control", "no-transform, max-age=0");

 

但是对于我这种情况也没用,然后我试着在IE6的浏览器里直接输入下载地址能正常下载,说明问题出在form提交这种方式上,于是我改成用iframe直接链接的方式,代码如下:

 

var iframe=$("<iframe style='display:none'>");
$('body').append(iframe);
iframe.attr('src',_url);

 OK,解决了,至于为什么用form提交方式不行,网上有人说原因如下:

 

Stupid ie6.

We found the problem and it is not related with java side of code.

The problem is because of this html statement in jsp:

This statement somehow creates 2 different request with 1 click on ie6, so this makes confusion on java side.

 

你可能感兴趣的:(ajax下载文档)