日期:2014-05-17  浏览次数:20774 次

jsp文件下载问题:Connection reset by peer: socket write error
我用以下方法实现资源下载功能:

首先是一个客户端JSP页面,显示可下载的资源列表,当用户点击某个资源是,传递一个唯一的资源编号到Action,在数据库中读取该资源的url,然后下载次数+1,然后把url放到一个request里面,再跳到一个JSP页面:

HTML code

<%@ page language="java" pageEncoding="GB18030"%>
<jsp:directive.page import="java.net.URLEncoder"/>
<%
    request.setCharacterEncoding("GB18030");
    String srcURL = String.valueOf(request.getAttribute("srcURL"));//资源路径

       response.setContentType("application/x-download");
       String url = "/upload/doc/"+srcURL;
       String fileName = srcURL;
       fileName = URLEncoder.encode(fileName, "UTF-8");
       response.addHeader("Content-Disposition", "attachment;filename="
               + fileName);
       RequestDispatcher rd = application.getRequestDispatcher(url);
       rd.forward(request, response);
       response.flushBuffer();
       out.clear();
       out.clearBuffer();
       out = pageContext.pushBody();
%>




这时候,可以下载资源了,但是我发现点击某些资源,弹出保存框,并且取消保存的时候,控制台输出如下错误:

Java code

2009-8-26 22:06:58 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:750)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:432)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:347)
    at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:773)
    at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:118)
    at org.apache.coyote.http11.InternalOutputBuffer.doWrite(InternalOutputBuffer.java:583)
    at org.apache.coyote.Response.doWrite(Response.java:560)
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:361)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:432)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:347)
    at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:392)
    at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:381)
    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)
    at org.apache.catalina.servlets.DefaultServlet.copyRange(DefaultServlet.java:2083)
    at org.apache.catalina.servlets.DefaultServlet.copy(DefaultServlet.java:1796)
    at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:874)
    at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:376)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.j