使用 Netty 提供文件 - 响应被截断一个字节

发布于 2024-12-26 04:03:52 字数 1853 浏览 2 评论 0原文

我通过 Netty 服务器从 Android 资产提供文件(图像、html)。 诸如 html 之类的文本文件保存为 .mp3 以禁用压缩(我需要一个输入流!)

我的管道如下所示:

    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());

    pipeline.addLast("handler", new AssetsServerHandler(context));

我的处理程序是:

public class AssetsServerHandler extends SimpleChannelUpstreamHandler {

    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

        // some checks

        final FileInputStream is;
        final AssetFileDescriptor afd;
        try {
            afd = assetManager.openFd(path);
            is = afd.createInputStream();   
        } catch(IOException exc) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        final long fileLength = afd.getLength();

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);

        final Channel ch = e.getChannel();
        final ChannelFuture future;
        ch.write(response);
        future = ch.write(new ChunkedStream(is));
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                future.getChannel().close();
            }
        });
        if (!isKeepAlive(request)) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
    // other stuff
}

使用该处理程序,我的响应至少被截断了一个字节。如果我将 ChunkedStream 更改为 ChunkedNioFile (因此使用 is.getChannel() 而不是 is 作为构造函数到它) - 一切都很完美。

请帮助我理解 ChunkedStream 有什么问题。

I've serving a files from Android assets via Netty server (images, html).
Text files such a html is saved as .mp3 to disable compression (I need an InputStream!)

My pipeline is looking like this:

    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());

    pipeline.addLast("handler", new AssetsServerHandler(context));

My handler is:

public class AssetsServerHandler extends SimpleChannelUpstreamHandler {

    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

        // some checks

        final FileInputStream is;
        final AssetFileDescriptor afd;
        try {
            afd = assetManager.openFd(path);
            is = afd.createInputStream();   
        } catch(IOException exc) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        final long fileLength = afd.getLength();

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);

        final Channel ch = e.getChannel();
        final ChannelFuture future;
        ch.write(response);
        future = ch.write(new ChunkedStream(is));
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                future.getChannel().close();
            }
        });
        if (!isKeepAlive(request)) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
    // other stuff
}

With that handler i've got my resposes truncated by at least one byte. If I change ChunkedStream to ChunkedNioFile (and so use a is.getChannel() instead of is as a constructor to it) - everything works perfectly.

Please, help me understand what is wrong with ChunkedStream.

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

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

发布评论

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

评论(1

多情出卖 2025-01-02 04:03:52

你的代码对我来说看起来很合适。 AssetFileDescriptor 返回的 FileInputStream 是否包含“所有字节”?您可以通过单元测试来检查这一点。如果其中没有错误,那么它是netty中的错误。我大量使用 ChunkInputStream,但从未遇到过这样的问题,但也许这实际上取决于 InputStream 的性质。

如果您可以编写一个测试用例并在 netty 的 github 上提出问题,那就太好了。

Your code looks right to me. Does the returned FileInputStream of AssetFileDescriptor contain "all the bytes" ? You could check this with a unit test. If there is no bug in it then its a bug in netty. I make heavy use of ChunkInputStream and never had such a problem yet, but maybe it really depends on the nature of the InputStream.

Would be nice if you could write a test case and open a issue at netty's github.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文