HTTP标头为未知的Content-Length [英] HTTP Headers for Unknown Content-Length

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

问题描述

我目前正在一个转码过程后,将内容传输到了Web。这通常可很好地写入二进制出我的网络流,但一些浏览器(特别是IE7,IE8)不喜欢没有在HTTP头文件中定义的内容长度。我认为,有效头都应该有这样的设置。

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.

什么是正确的方式将内容传输到网络,当你有一个未知的内容长度?在转码过程可能需要一段时间,所以我要开始它,因为它完成流出来了。

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.

推荐答案

尝试与<发送它们的块href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41"><$c$c>Transfer-Encoding: 分块 。在维基。

更新作为按照意见,这里有一个例子在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 = "\r\n".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(....);

您看到在源代码中,数据的每块内的再presents数据的十六进制的长度,CRLF,实际的数据和CRLF一个头的存在。流的结尾是一个头表示psented重新$ P $ 0的长度和两个CRLFs。

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应用。每当内容长度在响应设置时,Web容器会自动调用他们的块。

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标头为未知的Content-Length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆