non*_*ont 3 streaming spring transfer-encoding
我正在通过在Apache Tomcat/6.0.18上运行的Spring MVC控制器传输大型文档
因为它很大,并且(最终)会动态生成,所以我决定使用分块传输编码.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.ChunkedOutputStream;
import org.apache.commons.net.io.CopyStreamException;
import org.apache.commons.net.io.Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class QueryController {
@Inject
QueryService queryService;
@RequestMapping(value = "/stream")
public void hellostreamer(HttpServletResponse response) throws CopyStreamException, IOException {
response.setHeader("Transfer-Encoding", "chunked");
response.setHeader("Content-type", "text/xml");
InputStream filestream = new FileInputStream("/lotsrecs.xml");
ChunkedOutputStream chunkStream = new ChunkedOutputStream(response.getOutputStream());
Util.copyStream(filestream,chunkStream);
chunkStream.close();
chunkStream.finish();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在firefox中打开它时,我得到了这个:
XML Parsing Error: syntax error
Location: http://localhost:8082/streaming-mockup-1.0-SNAPSHOT/stream
Line Number 1, Column 1:
800
^
Run Code Online (Sandbox Code Playgroud)
而不是将块大小作为关于流的元数据读取,而是将它们作为流的一部分读取!
使用Live HTTP标头,我可以看到正在接收Transfer-Encoding标头:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Content-Type: text/xml
Date: Thu, 11 Aug 2011 18:08:07 GMT
Run Code Online (Sandbox Code Playgroud)
所以我不知道为什么没有正确解释块大小.如果我使用wget发出请求,我也会在返回的文档中看到块大小的字符,所以不知何故它们没有被正确编码.任何人都知道为什么?
使用wireshark查看传输:(请注意,"800"在整个流中重复出现)请注意0x800 = 2048,这是ChunkedOutputStream类使用的默认chunksize.
GET /streaming-mockup-1.0-SNAPSHOT/stream HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: localhost:8082
Connection: Keep-Alive
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Content-Type: text/xml
Date: Thu, 11 Aug 2011 18:47:24 GMT
Connection: close
800
<records>
<REC>
<FUID>412286284WOS1</FUID>
<UID>WOS:000292284100013</UID>
<static_data>
<summary>
<EWUID uid="WOS:000292284100013" year="2011">
Run Code Online (Sandbox Code Playgroud)
如果我只是直接复制到输出流而不创建ChunkedOutputStream,我根本看不到块大小:
GET /streaming-mockup-1.0-SNAPSHOT/stream HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: localhost:8082
Connection: Keep-Alive
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Content-Type: text/xml
Date: Thu, 11 Aug 2011 18:51:05 GMT
Connection: close
<records>
<REC>
<FUID>412286284WOS1</FUID>
<UID>WOS:000292284100013</UID>
<static_data>
<summary>
Run Code Online (Sandbox Code Playgroud)
那么我怎么知道这是否被分块?如果是的话,我不会看到块大小吗?
Hen*_*olm 11
你确定你需要ChunkedOutputStream为自己构建一个吗?
我的理解(没有实践)是ServletResponse.getOutputStream()应该在适当的时候为你处理分块(比如,如果客户端不是HTTP 1.0,等等).如果是真的,实际上被发送的答复,将分块编码中块编码,当然浏览器知道大约只有这些层.
您是否尝试在网络中的某个位置运行服务器并使用Wireshark检查事务?
更新:
GET /streaming-mockup-1.0-SNAPSHOT/stream HTTP/1.0
Run Code Online (Sandbox Code Playgroud)
HTTP/1.0客户端根本不需要理解分块编码(当然,因为该编码仅为1.1发明).
| 归档时间: |
|
| 查看次数: |
12864 次 |
| 最近记录: |