@RestController("/")
public class SimpleRestController {
@RequestMapping(produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, method = RequestMethod.GET)
public ResponseEntity<byte[]> getVideo(@RequestHeader(value = "Range", required = false) String range) throws IOException {
ClassPathResource video = new ClassPathResource("static/Jiraya.mp4");
InputStream is = video.getInputStream();
HttpHeaders headers = new HttpHeaders();
int fileSize = is.available();
byte[] data = new byte[fileSize];
String[] rangeArray = range.split("-");
int byteStart = Integer.parseInt(rangeArray[0].substring(6));
int byteEnd = fileSize - 1 - byteStart;
if (rangeArray.length > 1){
byteEnd = Integer.parseInt(rangeArray[1]);
}
headers.add("Content-Type", "video/mp4");
headers.add("Content-Length", String.valueOf(fileSize));
headers.add("Accept-Ranges", "bytes");
headers.add("Content-Range", "bytes" + " " +
byteStart + "-" + byteEnd + "/" + fileSize);
is.read(data, byteStart, byteEnd);
is.close();
return new ResponseEntity(data, headers, HttpStatus.PARTIAL_CONTENT);
}
}
@RestController("/")
public class SimpleRestController {
@RequestMapping(produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, method = RequestMethod.GET)
public ResponseEntity<byte[]> getVideo(@RequestHeader(value = "Range", required = false) String range) throws IOException {
ClassPathResource video = new ClassPathResource("static/Jiraya.mp4");
InputStream is = video.getInputStream();
HttpHeaders headers = new HttpHeaders();
int fileSize = is.available();
byte[] data;
String[] rangeArray = range.split("=");
int byteStart = 0;
int byteEnd = fileSize - 1;
if (rangeArray.length > 1){
String[] byteRange = rangeArray[1].split("-");
byteStart = Integer.parseInt(byteRange[0]);
if (byteRange.length > 1) {
byteEnd = Integer.parseInt(byteRange[1]);
}
}
int contentLength = byteEnd - byteStart + 1;
data = new byte[contentLength];
is.skip(byteStart);
is.read(data, 0, contentLength);
is.close();
headers.add("Content-Type", "video/mp4");
headers.add("Content-Length", String.valueOf(contentLength));
headers.add("Accept-Ranges", "bytes");
headers.add("Content-Range", "bytes " + byteStart + "-" + byteEnd + "/" + fileSize);
return new ResponseEntity(data, headers, HttpStatus.PARTIAL_CONTENT);
}
}