小编Sha*_* Qi的帖子

Spring MVC StreamingResponseBody返回chunked文件

我遇到了一个使用springMVC StreamingResponseBody返回视频的问题.我从服务器上获得的视频被打断了.你们能给我一个暗示吗?

视频文件被分块了

在此输入图像描述

我的控制器:

    @RestController
    @RequestMapping("/rest/videos")
    public class VideoController {
        private String videoLocation = "videos";
        private ConcurrentHashMap<String, File> videos = new ConcurrentHashMap<String, File>();

        @PostConstruct
        public void init() {
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            File dir = new File(classloader.getResource(videoLocation).getPath());
            System.out.println(dir);
            videos.clear();
            videos.putAll(Arrays.asList(dir.listFiles()).stream()
                    .collect(Collectors.toMap((f) -> {
                        String name = ((File) f).getName();
                        return name;
                    }, (f) -> (File) f)));
        }

        @RequestMapping(method = RequestMethod.GET, value = "/{video:.+}")
        @PreAuthorize("permitAll")
        public StreamingResponseBody stream(@PathVariable String video)
                throws FileNotFoundException {
            File videoFile = videos.get(video);
            final InputStream videoFileStream = new …
Run Code Online (Sandbox Code Playgroud)

java streaming tomcat spring-mvc maven

5
推荐指数
1
解决办法
3506
查看次数

标签 统计

java ×1

maven ×1

spring-mvc ×1

streaming ×1

tomcat ×1