首页 > 解决方案 > 休息:如何流式传输视频(不下载)

问题描述

这是将视频下载到磁盘而不是流式传输视频(这是我想要的)的代码。

@GET
@Path("/articleVideo/{videoID}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getArticleVideo(@PathParam("videoID") int videoID) {

    MediaDAO mediaDAO = new MediaDAO();
    MediaEntity video = mediaDAO.getMediaById(videoID);

    if(video == null) {
        throw new WebApplicationException("Requested user not found");
    }

    File file = new File(video.getLink());

    if (!file.exists()) {
        throw new WebApplicationException(404);
    }

    StreamingOutput out = new StreamingOutput() {
        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            try {
                InputStream input = new FileInputStream(file);
                IOUtils.copy(input, output);
                output.flush();
            } 
        }
    };

    return Response.ok(out).build();
}

我尝试了很多组合,并在此处查找了许多其他有关堆栈溢出的帖子,但没有一个有效。

标签: restvideojerseyvideo-streaming

解决方案


推荐阅读