我正在尝试从服务器的文件系统中传输视频。我使用Spring的StreamingResponseBody作为响应返回。因此,我必须异步处理流。我做了很多关于如何配置我的web.xml和Spring的研究,我遵循了这个简单的教程:http://shengwangi.blogspot.de/2015/09/asynchronous-spring-mvc-hello-world.html,但不幸的是没有成功!我仍然得到异常,告诉我“必须在servlet上启用异步支持,对于异步请求处理中涉及的所有过滤器都启用异步支持。这是在Java代码中使用Servlet实现的,或者通过在servlet和filter声明中添加"true”来实现的,尽管我认为我做得很好。下面是我在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: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();
}我很感激任何帮助,因为我三天前还在挣扎!我使用Tomcat8.0和Spring4.3.6
发布于 2017-10-29 13:20:38
我读过许多线程,表明它是Tomcat 7或8中的一个bug,但它并不是,异常清楚地表明,必须在所有过滤器中启用异步支持。由于我尝试这样做,但问题是如何在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>https://stackoverflow.com/questions/46621190
复制相似问题