额外的字符被添加到通过 Spring @RestController 下载的视频文件中

问题描述

我正在通过 Spring @RestController 下载文件,返回 ResponseEntity 对象的 OutputStream 中的视频字节。

@GetMapping(value = "/video/{id}/data")
    @ResponseBody
    public ResponseEntity<OutputStream> getVideoData(@PathVariable("id") Long id,HttpServletResponse response) {
        try {
            for (Video video : videos) {
                if (id == video.getId()) {
                    if (videoDataMgr == null) {
                        videoDataMgr = VideoFileManager.get();
                    }
                    videoDataMgr.copyVideoData(video,response.getoutputStream());
                    return new ResponseEntity<OutputStream>(response.getoutputStream(),HttpStatus.OK);
                }
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (IOException ioe) {
            logger.log (Level.WARNING,ioe.toString());
        }
        return null;
    }

如果我播放正在提供的视频和下载的视频,它们都会显示相同的内容。但是,逐字节比较 2 个 MP4 文件内容时,以下测试失败:

@Test
    public void testAddVideoData() throws Exception {
        Video received = videoSvc.addVideo(video);
        VideoStatus status = videoSvc.setVideoData(received.getId(),new TypedFile(received.getContentType(),testVideoData));
        assertEquals(VideoState.READY,status.getState());
        
        Response response = videoSvc.getData(received.getId());
        assertEquals(200,response.getStatus());
        
        InputStream videoData = response.getBody().in();
        byte[] originalFile = IoUtils.toByteArray(new FileInputStream(testVideoData));
        byte[] retrievedFile = IoUtils.toByteArray(videoData);
        assertTrue(Arrays.equals(originalFile,retrievedFile));
    }

文件之间进行比较我可以看到视频几乎相同,除了末尾的一些字符(下载文件内容在右侧):

Comparison between files

{"ready": false} 来自哪里?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)