java - 如何在不丢失元数据的情况下从 InputStream 保存视频文件?
问题描述
我有一个方法,它接受形式的文件InputStream
并将其InputStream
返回给用户。当用户从视频文件中保存InputStream
视频文件时无法播放。接收和返回文件的方法如下所示:
@RequestMapping(value = "/file_redirect", method = RequestMethod.POST)
public ResponseEntity fileRedirect(HttpServletRequest request) throws Exception{
InputStreamResource inputStreamResource = new InputStreamResource(request.getInputStream());
return new ResponseEntity(inputStreamResource, HttpStatus.OK);
}
我curl
用来发送请求和接收文件:
curl -X POST -H "content-length: 389907412" -H "Content-Type: multipart/form-data" -F "data=@/path/to/file/myVideo.mp4" -o returnedVideo.mp4 localhost/file_redirect
我也试过这个方法(文件大小是正确的):
@RequestMapping(value = "/file_redirect", method = RequestMethod.POST)
public ResponseEntity fileRedirect(HttpServletRequest request) throws Exception{
InputStreamResource inputStreamResource = new InputStreamResource(request.getInputStream());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentLength(389907412);
httpHeaders.setContentType(new MediaType("video", "mp4"));
return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
}
两种方法都“有效”并且文件已成功保存,但之后无法播放。返回文件的文件大小是正确的。返回文件的元数据丢失。原始文件和返回文件的文件类型均为MPEG-4 video (video/mp4)
. 原始文件和返回文件的校验和不同。
保存文件时我做错了什么?为什么元数据会在返回的文件中丢失?
解决方案
问题出在curl
请求本身而不是控制器中。似乎数据是作为一个字段发送的data
,这就是为什么当我在控制器中获取 InputStream 时, InputStream 包含data
带有值(文件)本身的字段。要发送没有字段的数据,我们需要使用下一个命令:
curl -X POST -H "content-length: 389907412" --data-binary "@/path/to/file/myVideo.mp4" -o returnedVideo.mp4 localhost/file_redirect
推荐阅读
- html - TikTok 的应用内浏览器未加载视频的 html/css 代码
- typescript - 如何在 TypeScript 中导出增强类型(扩展)
- applescript - 如何使用 AppleScript for BBEdit 替换选择?
- javascript - 消除两个内联块 DIV 之间的 1px 空间(已经尝试过标准解决方案)
- rust - NEAR 跨合约调用异常如何处理?
- codenameone - 是否有比 6.5.1 更稳定的代号 1 版本?还是我做错了什么?
- networking - 当你取消一个网络请求时究竟会发生什么?
- python - 如何将 webdriver 类型代码作为整数
- css - 部署后 Vuejs CSS 不起作用,即使它被渲染到 DOM
- spring-boot - 在 Spring Boot 中将多部分文件转换为字符串失败