java spring MVC中的视频流速度很慢

时间:2017-12-06 08:44:38

标签: java spring spring-mvc tomcat video-streaming

@RequestMapping(value = "/video/{clientID}/{fileName}", method = RequestMethod.GET)
public ResponseEntity<StreamingResponseBody> getClientVideo(@PathVariable(value = "clientID") Integer clientID, @PathVariable(value = "fileName") final String fileName) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    String absolutePath = new File(".").getAbsolutePath();
    File file = new File(Paths.get(absolutePath).getParent() + "/" + clientID);
    if (null != file) {
        FilenameFilter beginswithm = new FilenameFilter() {
            public boolean accept(File directory, String filename) {
                return filename.contains("ClientVideo_"+fileName);
            }
        };
        File[] files = file.listFiles(beginswithm);
        if (null != files && files.length > 0) {
            Resource resource = null;
            for (final File f : files) {
                headers.set("Content-Disposition", "inline; filename=" + f.getName());

                StreamingResponseBody responseBody = new StreamingResponseBody() {
                    @Override
                    public void writeTo(OutputStream out) throws IOException {
                        out.write(Files.readAllBytes(f.toPath()));
                        out.flush();
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_OCTET_STREAM).body(responseBody); //(responseBody, headers, HttpStatus.OK);
            }
        }
    }
    RecruiterResponseBean resBean = new RecruiterResponseBean();
    resBean.setStatusMessage("Video is not present : " + Constants.FAILED);
    resBean.setStatusCode(Constants.FAILED_CODE);
    return new ResponseEntity(HttpStatus.NOT_FOUND);
}

视频流正在运行,但速度非常慢。如何提高效率? 互联网没有问题,它是10mbps。我正在使用tomcat 7和Spring MVC [4.2.4]。我应该更改tomcat容量还是如何解决?我没有进入谷歌。

0 个答案:

没有答案