下载PDF文件 - 套接字错误

时间:2014-06-05 20:01:54

标签: java file download

我有一点问题,我有这种方法可以下载两种格式的文件(.txt和.pdc)。

对于文件.Txt没有问题。

但是对于PDF文件,当我取消下载或打开下载窗口而我没有采取任何行动时,代码就是这样的例外:

等待用户操作的时间:

java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:99)
at java.net.SocketOutputStream.write(SocketOutputStream.java:137)
at weblogic.servlet.internal.ChunkOutput.writeChunkTransfer(ChunkOutput.java:578)
at weblogic.servlet.internal.ChunkOutput.writeChunks(ChunkOutput.java:548)
at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:436)
at weblogic.servlet.internal.ChunkOutput$2.checkForFlush(ChunkOutput.java:657)
at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:342)
at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:148)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:151)
at java.io.OutputStream.write(OutputStream.java:59)
at FIRST.handlers.FRC.downloadFile(FRC.java:752)
at FIRST.handlers.FRC.handleDownloadPDFItem(FRC.java:638)
at FIRST.handlers.FRC.handleRequest(FRC.java:368)
at FIRST.FIRSTApplication.serviceRequest(FIRSTApplication.java:47)
at FIRST.FIRSTONLINE.service(FIRSTONLINE.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:845)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:236)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2091)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

当用户取消下载文件时:

java.net.SocketException: socket write error: Connection reset by peer.
at jrockit.net.SocketNativeIO.writeBytesPinned(Native Method)
at jrockit.net.SocketNativeIO.socketWrite(SocketNativeIO.java:46)
at java.net.SocketOutputStream.socketWrite0(SocketOutputStream.java)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at weblogic.servlet.internal.ChunkOutput.writeChunkTransfer(ChunkOutput.java:577)
at weblogic.servlet.internal.ChunkOutput.writeChunks(ChunkOutput.java:548)
at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:436)
at weblogic.servlet.internal.ChunkOutput$2.checkForFlush(ChunkOutput.java:657)
at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:342)
at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:148)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:151)
at java.io.OutputStream.write(OutputStream.java:58)
at FIRST.handlers.FRC.downloadFile(FRC.java:752)
at FIRST.handlers.FRC.handleDownloadPDFItem(FRC.java:638)
at FIRST.handlers.FRC.handleRequest(FRC.java:368)
at FIRST.FIRSTApplication.serviceRequest(FIRSTApplication.java:47)
at FIRST.FIRSTONLINE.service(FIRSTONLINE.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:235)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

这是我的方法:

private String downloadFile(String fileName, String name, String format) {
    String result = null;
    HttpServletResponse response = getHttpServletResponse();
    if (response != null) {

        if (format != null && format.contains(PDF_EXTENSION.getExtension())) {
            response.setContentType("application/pdf"); 
        } else {
            response.setContentType("application/text");
        }
        response.addHeader("Content-disposition", "attachment; filename=" + name);

        FileInputStream fs = null;
        try {
            OutputStream os = response.getOutputStream();
            fs = new FileInputStream(fileName);
            byte[] b = new byte[8192];
            byte[] crlf = { 0x0d, 0x0a };
            int size = 0;

            while ((size = fs.read(b)) != -1) {
                if (format == null) {
                    for (int i = 0; i < size; i++) {
                        if (b[i] == 0x0a) {
                            os.write(crlf);
                        } else {
                            os.write(b, i, 1);
                        }
                    }
                } else {
                    os.write(b);
                }
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fs != null) {
                    fs.close();
                }

                if(isPDFFile(fileName)) {
                    deletePDFFileTemporary(fileName);
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        result = "application/text";
    }
    return result;
}

感谢您的帮助。谢谢

0 个答案:

没有答案