我正在通过spring@restcontroller下载一个文件,返回responseentity对象输出流中的视频字节。
@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));
}
在对文件进行比较时,我可以看到除了结尾的一些字符(下载文件的内容在右侧),视频几乎是相同的:
{“ready”:false}从何而来?
暂无答案!
目前还没有任何答案,快来回答吧!