spring mvc框架上流式apis的问题

时间:2016-02-04 18:53:52

标签: java html5 spring spring-mvc streaming

我正在编写一个Spring启动应用程序,它具有REST apis(使用spring mvc框架),可以将音频/视频流式传输到浏览器上的HTML5播放器。这些apis支持对内容的范围请求。

我遇到了一个问题,HTML5视频播放器在流式传输过程中会定期抱怨错误ERR_CONTENT_LENGTH_MISMATCH。

从服务器接收的字节似乎与Content-Length标头中服务器公布的字节不匹配。

请告知可能是什么原因。

到目前为止我研究过的东西可以解决这个问题,但在我的案例中却没有:

没有响应缓冲。 在tomcat面前没有apache。

这是我的代码:

@Api("Player API")

@RestController public class PlayerController {

@Autowired
FetchAssetService fetchAssetService;

@ApiOperation("Get video")
@RequestMapping(value = "player/video/{packageId}/{username}", method = RequestMethod.GET)
public ResponseEntity<StreamingResponseBody> getProxy(@RequestHeader(value="Range", required=false) String range, @PathVariable Long packageId, @PathVariable String username) throws Exception {
    Optional<Stream> videoAssetMetaData = fetchAssetService.fetchVideoAssetMetaData(packageId);
    if (!videoAssetMetaData.isPresent()) {
        throw new AssetNotFoundException("Video asset not found in MPL for package: "+packageId);
    }
    HttpHeaders httpHeaders = new HttpHeaders();
    HttpStatus status = HttpStatus.OK;
    Optional<AssetRange> optionalAssetRange = AssetRange.create(range,videoAssetMetaData.get().getLength());
    if (optionalAssetRange.isPresent()) {
        if (optionalAssetRange.get().isSatisfiable()) {
            setSuccessRangeHeaders(httpHeaders,optionalAssetRange.get());
            status = HttpStatus.PARTIAL_CONTENT;
        } else {
            setErrorRangeHeaders(httpHeaders,optionalAssetRange.get());
            status = HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
            return new ResponseEntity(null,httpHeaders,status);
        }
    }
    setContentHeaders(httpHeaders, “video.mp4");
    try {
        return new ResponseEntity(fetchAssetService.getStreamingResponseBody(packageId,videoAssetMetaData.get(),optionalAssetRange,username),
                httpHeaders,
                status);
    } catch (Exception ex) {
        log.error("Exception while video streaming: package={}, user={}, range={}",packageId,username,range,ex);
        throw ex;
    }
}

private void setContentHeaders(HttpHeaders httpHeaders, String fileName) {
    httpHeaders.add(HttpHeaders.ACCEPT_RANGES,"bytes");
    httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
    httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename="+ fileName);
}

private void setSuccessRangeHeaders(HttpHeaders httpHeaders, AssetRange range) {
    httpHeaders.add(HttpHeaders.CONTENT_LENGTH, Long.toString(range.getRangeLength()));
    httpHeaders.add(HttpHeaders.CONTENT_RANGE, String.format("bytes %d-%d/%d", range.getStart(), range.getEnd(), range.getTotalLength()));
}

private void setErrorRangeHeaders(HttpHeaders httpHeaders, AssetRange range) {
    httpHeaders.add(HttpHeaders.CONTENT_RANGE, String.format("bytes */%d", range.getTotalLength()));
}

@ExceptionHandler(AssetNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleAppException(AssetNotFoundException ex) {
    return ex.getMessage();
}

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleAppException(Exception ex) {
    return ex.getMessage();
}

}

1 个答案:

答案 0 :(得分:0)

最好的猜测, 在setSuccessRangeHeaders中,您将内容长度设置为范围值而不是响应的实际内容长度。

尝试不设置content_length或尝试更准确地设置它。

这可能会有所帮助: How to set content length as long value in http header in java?