我尝试从服务器的文件系统中流式传输视频。我使用Spring的StreamingResponseBody作为响应返回。所以我必须异步处理流媒体。 我做了很多关于如何配置我的web.xml和Spring MVC的研究,我遵循了这个简单的教程:http://shengwangi.blogspot.de/2015/09/asynchronous-spring-mvc-hello-world.html 但不幸的是没有成功! 我仍然得到异常告诉我"必须在servlet和异步请求处理中涉及的所有过滤器上启用异步支持。这是使用Servlet API在Java代码中完成的,或者通过添加" true" to weblet"中的servlet和过滤声明尽管我认为我做的一切都是正确的。 这是我在web.xml中的servlet配置
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<filter>
<display-name>springSecurityFilterChain</display-name>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
Spring MVC配置的代码片段:
<mvc:annotation-driven>
<mvc:async-support default-timeout="30000" task-executor="taskExecutor">
</mvc:async-support>
</mvc:annotation-driven>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="50" />
<property name="queueCapacity" value="10" />
<property name="keepAliveSeconds" value="120" />
</bean>
这是请求处理程序:
@GetMapping("/videos/play")
public StreamingResponseBody stream(@RequestParam("code") String code, @RequestParam("locale") String locale) throws FileNotFoundException{
File video = videoService.getVideo(code, locale);
final InputStream videoInputStream = new FileInputStream(video);
return (outputStream) -> {
copyToStream(videoInputStream, outputStream);
};
}
private void copyToStream(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) > 0) {
os.write(data, 0, read);
}
os.flush();
}
我感谢任何帮助,因为我在3天前挣扎了! 我使用Tomcat8.0和Spring 4.3.6
答案 0 :(得分:1)
我读了许多线程,这些线程表明它是Tomcat 7或8中的一个错误,但它没有。这个例外表明必须在所有过滤器中启用异步支持。因为我试图这样做,但问题是我如何向web.xml文件中的过滤器添加异步支持。 我做的是以下
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
这实际上告诉我映射接受ASYNC请求(我猜),但它不会在过滤器本身中启用异步支持。 因此,在每个过滤器定义中,我们应该启用异步支持,因此springSecurityFilterChain定义变为:
<filter>
<display-name>springSecurityFilterChain</display-name>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
</filter>