gpt4 book ai didi

java - ReSTLet - 使用 StreamingOutput 的 StreamClosedException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:02 27 4
gpt4 key购买 nike

我的 ReSTLet 资源遇到问题。它使用 TrueZip 构建 Zip 存档的子集并允许用户下载它。

// Create a Streaming Response Entity.
final StreamingOutput stream = new StreamingOutput() {
@Override
public void write(final OutputStream output) {
ZipBrowser.extract(source, path, output);
}
};
LOGGER.debug("Download of Path {} with the length {} initiated", path, length);
ResponseBuilder rb = Response.ok(stream);
rb.header(HeaderConstants.HEADER_CONTENT_DISPOSITION, CONDISPOVALUE + fileName);
rb.header(HeaderConstants.HEADER_CONTENT_LENGTH, length);
return rb.build();

尽管它有效,但我收到了一个恼人的 StreamClosedException。只有当我尝试下载一个子集而不是整个 Zip 存档时才会出现此错误:

An exception occured writing the responseentity

sun.net.httpserver.StreamClosedException
at sun.net.httpserver.ChunkedOutputStream.flush(ChunkedOutputStream.java:156)
at sun.net.httpserver.PlaceholderOutputStream.flush(ExchangeImpl.java:449)
at org.restlet.engine.adapter.ServerCall.writeResponseBody(ServerCall.java:511)
at org.restlet.engine.adapter.ServerCall.sendResponse(ServerCall.java:454)
at org.restlet.engine.adapter.ServerAdapter.commit(ServerAdapter.java:187)
at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:144)
at org.restlet.engine.connector.HttpServerHelper$1.handle(HttpServerHelper.java:64)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:649)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)Unable to send error response

java.io.IOException: headers already sent

at sun.net.httpserver.ExchangeImpl.sendResponseHeaders(ExchangeImpl.java:204)
at sun.net.httpserver.HttpExchangeImpl.sendResponseHeaders(HttpExchangeImpl.java:86)
at org.restlet.engine.connector.HttpExchangeCall.writeResponseHead(HttpExchangeCall.java:148)
at org.restlet.engine.adapter.ServerCall.sendResponse(ServerCall.java:450)
at org.restlet.engine.adapter.ServerAdapter.commit(ServerAdapter.java:205)
at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:144)
at org.restlet.engine.connector.HttpServerHelper$1.handle(HttpServerHelper.java:64)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:649)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

最佳答案

这是由 zip 库和 ReSTLet Framework 在完成后试图关闭 OutputStream 引起的。

我过去在其他库中遇到过这种情况,并设法通过包装 OutputStream 来消除异常,我将传递给 zip 库的类覆盖了 close()什么也不做。委托(delegate)所有其他方法。然后这允许 ReSTLet 关闭流。

这样在您的代码中调用 Zip 实用程序的行就变成了:

 ZipBrowser.extract(source, path, new WrappedOutputStream(output));

WrappedOutputStream 类如下所示(需要添加委托(delegate)方法)。

import java.io.IOException;
import java.io.OutputStream;

public class WrappedOutputStream extends OutputStream {

private final OutputStream delegate;

public WrappedOutputStream(final OutputStream delegate) {
this.delegate = delegate;
}

public void close() throws IOException {
// Do Nothing to allow Restlet to close the underlying stream
}

// TODO Delegate other Outpt Stream methods.
}

关于java - ReSTLet - 使用 StreamingOutput 的 StreamClosedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35359355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com