未知内容长度的 HTTP 标头

HTTP Headers for Unknown Content-Length(未知内容长度的 HTTP 标头)

本文介绍了未知内容长度的 HTTP 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在转码过程后将内容流式传输到网络.这通常可以通过将二进制文件写入我的 Web 流来正常工作,但是某些浏览器(特别是 IE7、IE8)不喜欢在 HTTP 标头中没有定义 Content-Length.我相信有效"的标题应该有这个设置.

I am currently trying to stream content out to the web after a trans-coding process. This usually works fine by writing binary out to my web stream, but some browsers (specifically IE7, IE8) do not like not having the Content-Length defined in the HTTP header. I believe that "valid" headers are supposed to have this set.

当您的 Content-Length 未知时,将内容流式传输到网络的正确方法是什么?转码过程可能需要一段时间,所以我想在它完成后开始流式传输.

What is the proper way to stream content to the web when you have an unknown Content-Length? The trans-coding process can take awhile, so I want to start streaming it out as it completes.

推荐答案

尝试将它们与 传输编码: 分块.维基百科中的更多详细信息.

Try sending them in chunks along with Transfer-Encoding: chunked. More details in wikipedia.

更新根据评论,这是一个示例,Java 中的ChunkedOutputStream"可能如下所示:

Update as per the comments, here's an example how a "ChunkedOutputStream" in Java may look like:

package com.stackoverflow.q2395192;

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

public class ChunkedOutputStream extends OutputStream {

    private static final byte[] CRLF = "
".getBytes();
    private OutputStream output = null;

    public ChunkedOutputStream(OutputStream output) {
        this.output = output;
    }

    @Override
    public void write(int i) throws IOException {
        write(new byte[] { (byte) i }, 0, 1);
    }

    @Override
    public void write(byte[] b, int offset, int length) throws IOException {
        writeHeader(length);
        output.write(CRLF, 0, CRLF.length);
        output.write(b, offset, length);
        output.write(CRLF, 0, CRLF.length);
    }

    @Override
    public void flush() throws IOException {
        output.flush();
    }

    @Override
    public void close() throws IOException {
        writeHeader(0);
        output.write(CRLF, 0, CRLF.length);
        output.write(CRLF, 0, CRLF.length);
        output.close();
    }

    private void writeHeader(int length) throws IOException {
        byte[] header = Integer.toHexString(length).getBytes();
        output.write(header, 0, header.length);
    }

}

...基本上可以用作:

...which can basically be used as:

OutputStream output = new ChunkedOutputStream(response.getOutputStream());
output.write(....);

您在源代码中看到,每个数据块都存在一个标头,该标头表示十六进制数据的长度,一个 CRLF,实际数据和一个 CRLF.流的结尾由表示 0 长度和两个 CRLF 的标头表示.

You see in the source, every chunk of data exist of a header which represents the length of data in hex, a CRLF, the actual data and a CRLF. The end of the stream is represented by a header denoting a 0 length and two CRLFs.

注意:尽管有示例,但您实际上不需要在基于 JSP/Servlet 的 Web 应用程序中需要它.每当响应中没有设置内容长度时,webcontainer 会自动以块的形式传输它们.

Note: despite the example, you actually do not need it in a JSP/Servlet based webapplication. Whenever the content length is not set on a response, the webcontainer will automatically transfer them in chunks.

这篇关于未知内容长度的 HTTP 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:未知内容长度的 HTTP 标头

基础教程推荐