我試圖從服務器的文件系統中流式傳輸視頻。我使用Spring的StreamingResponseBody作爲響應返回。所以我不得不異步處理流媒體。 我做了很多關於如何配置我的web.xml和Spring MVC的研究,我遵循這個簡單的教程:http://shengwangi.blogspot.de/2015/09/asynchronous-spring-mvc-hello-world.html ,但不幸的是沒有成功! 我仍然收到異常告訴我:「必須在servlet上啓用異步支持,併爲異步請求處理中涉及的所有過濾器啓用異步支持。這可以在使用Servlet API的Java代碼中完成,或者在servlet和過濾器聲明中添加」true「 web.xml「儘管我認爲我做的一切都是正確的。 這裏是在web.xml我的servlet配置Servlet 3 Spring MVC的異步支持不起作用
<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的配置的代碼snipt:
<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
嗨 - 我面臨着類似的問題,並試圖您的加入過濾器的裝飾解決方案。但得到一個錯誤 - org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有名爲'springSecurityFilterChain'的bean可用你能請指導我嗎? – csharpnewbie