当我尝试从服务器流式传输视频文件时,我正面临servlet的这个问题。 服务文件的代码
if (contentType == null) {
contentType = "application/octet-stream";
}
response.reset();
response.setBufferSize(ServerConfiguration.DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
response.setHeader("Cache-Control", "public") ;
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(file), ServerConfiguration.DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), ServerConfiguration.DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[ServerConfiguration.DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
}
catch (Exception e) {
log(e);
}
finally {
// Gently close streams.
close(output);
close(input);
}
我已将内容处置设置为'inline',当我调试时,我可以看到请求以无限循环进入服务器,每次都会抛出此错误:
ClientAbortException: java.io.IOException
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:369)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:327)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:392)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:381)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:93)
at java.io.BufferedOutputStream.write(Unknown Source)
at com.venividi.ResourceManager.UploadFileManager.getMediaFiles(UploadFileManager.java:118)
at com.venividi.Servlet.VenividiServlet.doGet(VenividiServlet.java:182)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.venividi.Servlet.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:306)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1732)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException
at org.apache.coyote.http11.InternalAprOutputBuffer.flushBuffer(InternalAprOutputBuffer.java:208)
at org.apache.coyote.http11.InternalAprOutputBuffer$SocketOutputBuffer.doWrite(InternalAprOutputBuffer.java:238)
at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:84)
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:190)
at org.apache.coyote.Response.doWrite(Response.java:533)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:364)
... 28 more
如果我将内容处理类型更改为附件 - 我可以下载文件并从localsytem播放文件(这意味着数据正确)但无法播放此视频文件(通过网址)使用VLC播放器(网络流选项)或我的自定义iPhone媒体播放器。
我想做一些类似服务器的事情 - http://www.yo-yo.org/mp4/yu.mp4 ..我可以通过将URL提供给VLC或我的Iphone app ..网站来播放此文件(http://www.yo-yo .org / mp4 /)..
溜溜球mp4文件的响应标题是
接受-范围:字节 连接:保持活动 内容长度:7846115 内容类型:text / plain的;字符集= UTF-8 日期:2011年8月15日星期一18:01:12 GMT ETag的: “32a000f-77b8e3-3bd314df3c340” 保持活动:超时= 2 最后修改时间:2003年5月9日星期五01:39:49 GMT 服务器:Apache / 2.2.8(Fedora)
虽然我的服务器的响应头是
缓存控制:公共 内容处置:内联;文件名= “3648ef48-71bf-4393-9c0e-89fda68a683c.mp4” 内容长度:7846115 内容类型:视频/ MP4 日期:2011年8月15日星期一18:10:07 GMT 服务器:Apache-狼/ 1.1
请提供一些建议。我真的很感激任何帮助
此致 苏里亚
答案 0 :(得分:3)
我得到了上述问题的解决方案。 chrome发送字节范围请求,服务器应处理此类请求,而不是在单个请求中写入完整文件。
此链接很有用:Servlet Supporting Resume