这里是代码,而不是流式传输视频(这正是我想要的),而是将视频下载到磁盘上。
@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();
}
我尝试了多种组合,并在堆栈溢出中查找了许多其他帖子,但是它们都没有起作用。