Tomcat文件下载在IE中挂起

时间:2014-07-21 15:45:09

标签: tomcat servlets download

我正在尝试从Tomcat 7服务器下载Excel文件。一切都运行良好,直到大约2周前。那时,下载开始挂在IE(IE11)中。该下载也有效,仍可在Chrome中使用。超时后,我得到以下异常:

ClientAbortException:  java.io.IOException
    at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:371)
    at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:333)
    at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:101)
    at com.firstmanhattan.admin.model.PoiSpreadSheet.sendFile(PoiSpreadSheet.java:179)
    at com.firstmanhattan.admin.actions.BondDurationAction.perform(BondDurationAction.java:200)
    at com.firstmanhattan.servlet.FMCController.doPost(FMCController.java:147)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException
    at org.apache.coyote.http11.InternalAprOutputBuffer.flushBuffer(InternalAprOutputBuffer.java:205)
    at org.apache.coyote.http11.InternalAprOutputBuffer.flush(InternalAprOutputBuffer.java:109)
    at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:805)
    at org.apache.coyote.Response.action(Response.java:174)
    at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:366)
    ... 24 more

代码如下:

public void sendFile(HttpServletResponse response) {
    try {
        String fullname = "d:\\Temp\\myfile.xls";
        response.reset();

// tried both of the following lines - no difference. 
//      response.setContentType("application/vnd.ms-excel");
        response.setContentType("application/msexcel"); 
        response.setHeader("Content-Disposition", "attachment; filename="+filename);
        System.out.println("length = " + length);             // Displays 108544
        response.setContentLength((int)length);
        response.flushBuffer();
        ServletOutputStream o = response.getOutputStream();
        FileInputStream inStream = new FileInputStream(fullname);
        int read = -1; 

        byte[] bytes = new byte[4096]; 

        while((read = inStream.read(bytes)) != -1)  {
            System.out.println("read = " + read);
            o.write(bytes, 0, read);
            o.flush();     // tried with and without this
        } 
        System.out.println("Done");
        inStream.close();
        System.out.println("Stream closed");
        if (o != null) {
            o.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在Chrome上,写循环打印26个读取4096个字节和2048个(总计108544,正确的文件大小)。在IE上,循环打印出9个4096字节的读取然后挂起。最终超时抛出上述异常。

有什么想法吗?

0 个答案:

没有答案