I have a method which accepts a file in form of InputStream
and returns this InputStream
back to the user. When a user saves the video file back from the InputStream
the video file cannot be played.
The method which receives and returns the file looks like this one:
@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);
}
I'm using curl
to send request and receive the file:
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
I also tried this method (the file size is correct):
@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);
}
Both methods "works" and the file is saved successfully but it cannot be played after that. The file size of the returned file is correct. Metadata of the returned file is lost. The file type on both original and returned file is MPEG-4 video (video/mp4)
. Checksums of the original file and returned file are different.
What am I doing wrong when saving the file? Why the metadata is lost in the returned file?