none
Java中下载文件错误 RRS feed

  • 常规讨论

  • 采用Servlet下载文件时,我本机运行正常,能够下载,但在别人电脑上测试时,文件过大下载时后台出错,错误信息如下:

    org.mortbay.jetty.EofException
     at org.mortbay.jetty.HttpGenerator.flush(HttpGenerator.java:760)
     at org.mortbay.jetty.AbstractGenerator$Output.blockForOutput(AbstractGenerator.java:548)
     at org.mortbay.jetty.AbstractGenerator$Output.flush(AbstractGenerator.java:569)
     at org.mortbay.jetty.HttpConnection$Output.flush(HttpConnection.java:910)
     at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:646)
     at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:577)
     at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
     at java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
     at com.eintech.projectDeclare.web.DownloadProjectPackageServlet.doGet(DownloadProjectPackageServlet.java:61)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
     at org.eclipse.equinox.http.servlet.internal.ServletRegistration.handleRequest(ServletRegistration.java:90)
     at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:111)
     at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:59)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
     at org.eclipse.equinox.http.jetty.internal.HttpServerManager$InternalHttpServiceServlet.service(HttpServerManager.java:255)
     at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
     at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
     at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
     at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
     at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
     at org.mortbay.jetty.Server.handle(Server.java:324)
     at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
     at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:828)
     at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
     at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
     at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
     at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
     at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:450)
    Caused by: java.io.IOException: 您的主机中的软件放弃了一个已建立的连接。
     at sun.nio.ch.SocketDispatcher.write0(Native Method)
     at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:33)
     at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:104)
     at sun.nio.ch.IOUtil.write(IOUtil.java:60)
     at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:334)
     at org.mortbay.io.nio.ChannelEndPoint.flush(ChannelEndPoint.java:166)
     at org.mortbay.io.nio.SelectChannelEndPoint.flush(SelectChannelEndPoint.java:207)
     at org.mortbay.jetty.HttpGenerator.flush(HttpGenerator.java:693)
     ... 28 more

     

    下载程序源码如下:

     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
      String filePath = req.getParameter("filePath");
      if(filePath==null){
       resp.getWriter().write("{success:false,errorMessage:'文件路径为空!'}");
       return;
      }
      
      resp.reset();
      
      InputStream is = null;
      OutputStream os = null;
      try{
       String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
       resp.setContentType("APPLICATION/OCTET-STREAM;charset=utf8");
       resp.setHeader("Content-disposition", "attachment;filename="
         + new String(fileName.getBytes("gbk"), "iso8859-1") + "");
       
       is = new BufferedInputStream(new FileInputStream(filePath));
       os = new BufferedOutputStream(resp.getOutputStream());
       int len = 0;
       byte [] buf = new byte[2048];
       while((len=is.read(buf))>0){
        os.write(buf, 0, len);
       }
       os.flush(); 
      }catch (IOException e) {
          e.printStackTrace();
      } finally {
       try {
        if(is!=null){
         is.close();
        }
        if(os!=null){
         os.close();
        }
       }catch (IOException e) {
        e.printStackTrace();
          }
      }
     }


    喻敏华
    2010年4月3日 5:01

全部回复