How to stream media content from Spring Boot endpoint for javascript client
<p>I have a spring boot rest service with endpoints defined like this:</p>

<pre><code>@RequestMapping(value = "...", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) //and octet for videos
@ResponseBody
public ResponseEntity&lt;byte[]&gt; get(...) {
   //get image as byte array and return
}
</code></pre>

<p>And the client side in javascript (angular) :</p>

<pre><code>var headers = {headers : {..., "Accept":"image/jpeg"}}
var response = $http.get(...);
obj.imageSrc = 'data:image/jpg;base64,' + response;
obj.Image = new Image();
obj.Image.src = obj.imageSrc;
</code></pre>

<p><strong>What I require</strong></p>

<p>I thought this endpoint would behave like a regular image file on a file server so that the client can just do a get on it (for reasons outside the scope of this question, it can't actually be a raw image to access with a URL). Apparently this isn't the case - the byte array has to be converted to base64 (is there a way around this, since the array becomes much bigger), which has the effect of rotating the image. I wrongly thought getting an image from an API like this in javascript would be easy :(</p>

<p>Secondly, doing it this way means that javascript has to download the entire array. Is there a way I can define the endpoint to allow for a stream so that javascript can get it in chunks (I imagine both Spring and JS have utility functions that would handle this?)</p>

<p><strong>The basic underlying question</strong></p>

<p>How do I implement this properly in Spring so as to be as Javascript friendly as possible? Bearing in mind I want to serve both images (~5MB) and videos (~100MB)</p>

<p>Thanks in advance.</p>

<p><strong>EDIT</strong>
Perhaps I should be using FileResourceStream as the return type? No idea if this is the "right" way.</p>