Tomcat 下载流损坏

发布于 2024-11-08 06:44:23 字数 4090 浏览 0 评论 0原文

我有一个在 Tomcat 上运行的文件下载 servlet,代码是

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    byte buf[] = new byte[1024 * 4];
    try {
        File file = new File(realFilename);
        int size = (int) file.length();
        BufferedInputStream realFile = new BufferedInputStream(new FileInputStream(file));
        OutputStream out = response.getOutputStream();
        response.setContentLength(size);
        while (true) {
            int count = realFile.read(buf);
            if (count == -1) {
                break;
            }
            out.write(buf, 0, count);
        }
        out.flush();
    } catch (FileNotFoundException e) {
        logger.error("Stream download failed, file not found: " + realFilename, e);
        response.sendError(404);
    } catch (Exception e) {
        logger.error("DownloadException:", e);
    }
}

但是偶尔我会收到一个阻止文件下载的错误:

DownloadException:  java.net.SocketException: Broken pipe
        at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:366)
        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 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:113)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        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)
        ... 23 more

如何防止此类错误?

I have a file download servlet running on Tomcat, the code is

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    byte buf[] = new byte[1024 * 4];
    try {
        File file = new File(realFilename);
        int size = (int) file.length();
        BufferedInputStream realFile = new BufferedInputStream(new FileInputStream(file));
        OutputStream out = response.getOutputStream();
        response.setContentLength(size);
        while (true) {
            int count = realFile.read(buf);
            if (count == -1) {
                break;
            }
            out.write(buf, 0, count);
        }
        out.flush();
    } catch (FileNotFoundException e) {
        logger.error("Stream download failed, file not found: " + realFilename, e);
        response.sendError(404);
    } catch (Exception e) {
        logger.error("DownloadException:", e);
    }
}

However once in a while I get an error that prevents files from being downloaded:

DownloadException:  java.net.SocketException: Broken pipe
        at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:366)
        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 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:113)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        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)
        ... 23 more

How can I prevent such an error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記憶穿過時間隧道 2024-11-15 06:44:23

我读了你的代码,没有发现任何错误。但我有一个建议,也许这会起作用,因为类似的代码对我有用。

import org.apache.commons.io.IOUtils;

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    byte buf[] = new byte[1024 * 4];
    try {
        File file = new File(realFilename);
        OutputStream out = response.getOutputStream();
        IOUtils.copy(new FileInputStream(file), out);
        response.setHeader("Content-Disposition", "attachment; filename=\"" +
         realFileName+ "\"");
        response.setContentLength(file.length());
        out.flush();
    } catch (FileNotFoundException e) {
        logger.error("Stream download failed, file not found: " + realFilename, e);
        response.sendError(404);
    } catch (Exception e) {
        logger.error("DownloadException:", e);
    }
}

I read your code and haven't found anything wrong. But I have a suggestion, maybe this would work, as a similar code works for me.

import org.apache.commons.io.IOUtils;

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    byte buf[] = new byte[1024 * 4];
    try {
        File file = new File(realFilename);
        OutputStream out = response.getOutputStream();
        IOUtils.copy(new FileInputStream(file), out);
        response.setHeader("Content-Disposition", "attachment; filename=\"" +
         realFileName+ "\"");
        response.setContentLength(file.length());
        out.flush();
    } catch (FileNotFoundException e) {
        logger.error("Stream download failed, file not found: " + realFilename, e);
        response.sendError(404);
    } catch (Exception e) {
        logger.error("DownloadException:", e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文