如何使用Servlet来传输音频/视频文件,如MP3,MP4,AVI等 [英] How to stream audio/video files such as MP3, MP4, AVI, etc using a Servlet

查看:558
本文介绍了如何使用Servlet来传输音频/视频文件,如MP3,MP4,AVI等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用servlet来我的音频/视频文件传输到网络。

I would like to stream my audio/video files to web using servlet.

我做了如下的servlet的尝试:

I made an attempt with the following servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    File file = new File("/Users/myfolder/Documents/workspace/love.mp3");
    response.setContentType(getServletContext().getMimeType(file.getName()));
    response.setContentLength((int) file.length());
    Files.copy(file.toPath(), response.getOutputStream());
}

和下面的HTML:

<a href="/media" data-format="mp3 ogg">Click Here!</a>

不过,玩家只是加载中...加载中...载入中...

However, the player is just loading... loading... loading...

这是怎么造成的,我该怎么解决呢?

How is this caused and how can I solve it?

推荐答案

很多的媒体播放器需要服务器支持所谓的HTTP范围请求。即它必须能够根据要求返回媒体文件的特定部位具有的 范围 头。例如,只有在准确的索引1000,直到与2000上长10MB的一个文件中的字节。这是强制性的许多媒体播放器,以便能够迅速地跳过在一定范围的媒体流的足够和/或创建多个连接,其中每个请求文件的不同部分,以提高缓冲速度。

A lot of media players require that the server supports the so-called HTTP range requests. I.e. it must be able to return specific parts of the media file on request with a Range header. For example, only the bytes at exactly the index 1000 until with 2000 on a file of 10MB long. This is mandatory for many media players in order to be able to skip a certain range of the media stream quickly enough and/or to improve buffering speed by creating multiple connections which each requests different parts of the file.

这是但很多你的servlet额外code的需要的HTTP的 范围 规范。通常servletcontainer的(Tomcat的,JBoss AS中,Glassfish的,等等)的默认servlet已经支持这个开箱。所以,如果有发布媒体文件通过标准手段的网络,这样你就不需要homegrow这一个servlet的一种方式,那么我会走这条路的。

This is however a lot of additional code in your servlet which requires a well understanding of the HTTP Range specification. Usually the servletcontainer's (Tomcat, JBoss AS, Glassfish, etc) own default servlet already supports this out the box. So if there's a way to publish the media folder into the web by standard means, so that you don't need to homegrow a servlet for this, then I'd go on this route.

目前还不清楚这servletcontainer你使用,所以我会在这个例子中假设Tomcat的:

It's unclear which servletcontainer you're using, so I'll assume Tomcat in this example:


  1. 刚落,在Web项目的公开网页内容 love.mp3 文件,这样它只是可以通过&LT;一HREF =love.mp3&GT; 而不需要一个整体的servlet。

  1. Just drop love.mp3 file in the public web content of the web project, so that it's just available by <a href="love.mp3"> without the need for a whole servlet.

love.mp3 文件中的的Tomcat / webapps中文件夹中的一个新的子,例如的Tomcat / webapps /下媒体/ love.mp3 。这种方式是通过提供&LT; A HREF =/媒体/ love.mp3方式&gt;

Put the love.mp3 file in a new subfolder of Tomcat/webapps folder, e.g. Tomcat/webapps/media/love.mp3. This way it's available by <a href="/media/love.mp3">.

love.mp3 文件在磁盘上的其他地方,例如 /path/to/media/love.mp3 ,并通过添加以下行添加 /媒体文件夹作为新的上下文Tomcat的 /conf/server.xml

Put the love.mp3 file elsewhere on disk, e.g. /path/to/media/love.mp3 and add the /media folder as new context by adding the following line to Tomcat's /conf/server.xml:

<Context docBase="/path/to/media" path="/media" />

此方式,它可用以&LT; A HREF =/媒体/ love.mp3方式&gt; 以及

无论哪种方式,Tomcat的自己 DefaultServlet ,这对适当的支持范围 请求,将用于流内容。

Either way, Tomcat's own DefaultServlet, which has proper support for Range requests, will be used to stream the content.

但是,如果有绝对没有办法利用servletcontainer自己的默认servlet,那么你需要重写你的servlet code以这样一种方式,它正确支持的 范围 请求。您可以从开源的例子,如<一个获取灵感href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.tomcat/tomcat-catalina/7.0.0/org/apache/catalina/servlets/DefaultServlet.java\"相对=nofollow> Tomcat的 DefaultServlet 和<一个href=\"https://github.com/omnifaces/omnifaces/blob/master/src/main/java/org/omnifaces/servlet/FileServlet.java\"相对=nofollow> OmniFaces FileServlet

But if there's absolutely no way to make use of servletcontainer's own default servlet, then you need to rewrite your servlet code in such way that it properly supports Range requests. You can get inspiration from open source examples such as Tomcat DefaultServlet and OmniFaces FileServlet.

  • Video Using HTML 5 and servlet

这篇关于如何使用Servlet来传输音频/视频文件,如MP3,MP4,AVI等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆