从网页中的servlet读取quicktime电影?

时间:2010-05-31 06:04:39

标签: java html servlets video-streaming

我有一个servlet,它通过从服务器读取文件构建对媒体文件请求的响应:

 File uploadFile = new File("C:\\TEMP\\movie.mov");
 FileInputStream in = new FileInputStream(uploadFile);

然后将该流写入响应流。我的问题是如何使用embed或object标签在网页中播放媒体文件以从响应中读取媒体流?

这是我在servlet中的代码:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getParameter("location"); 
    uploadFile(response); 
}

private void uploadFile(HttpServletResponse response) {
    File transferFile = new File("C:/TEMP/captured.mov"); 
    FileInputStream in = null;

    try {
        in = new FileInputStream(transferFile);
    } catch (FileNotFoundException e) {
        System.out.println("File not found"); 
    }

    try {
        System.out.println("in byes i s" + in.available());
    } catch (IOException e) {
    }

    DataOutputStream responseStream = null;

    try {
        responseStream = new DataOutputStream(response.getOutputStream());
    } catch (IOException e) {
        System.out.println("Io exception"); 
    }

    try {
        Util.copyStream(in, responseStream);
    } catch (CopyStreamException e) {
        System.out.println("copy Stream exception"); 
    }

    try {
        responseStream.flush();
    } catch (IOException e) {
    }

    try {
        responseStream.close();
    } catch (IOException e) {
    }
}

这里是Ryan建议的html页面:

<embed SRC="http://localhost:7101/movies/transferservlet" 
    WIDTH=100 HEIGHT=196 AUTOPLAY=true CONTROLLER=true LOOP=false 
    PLUGINSPAGE="http://www.apple.com/quicktime/">

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

首先,它正在触发GET请求,但servlet仅侦听POST个请求。您需要使用doGet()方法而不是doPost()执行此任务。

您还需要向webbrowser指示您正在发送的确切信息。这是使用HTTP Content-Type标头完成的。您可以找到here概述最常用的内容类型(mime类型)。您可以使用HttpServletResponse#setContentType()进行设置。如果是Quicktime .mov文件,则内容类型应为video/quicktime

response.setContentType("video/quicktime");

此外,每种媒体格式都有自己的方式使用<embed>和/或<object>元素进行嵌入。您需要查阅媒体格式供应商的文档以获取有关如何使用它的详细信息。如果是Quicktime .mov文件,则需要咨询Apple。仔细阅读本文档。它写得很好,它也处理交叉浏览器的不一致。您可能希望Do It the Easy Way借助简单的JavaScript来透明地桥接交叉浏览器的不一致。

<script src="AC_QuickTime.js" language="javascript"> </script>
<script language="javascript">
    QT_WriteOBJECT('movies/filename.mov' , '320', '240' , '');
</script>

那就是说,发布的servlet代码老实说是可怕的写。除了错误地使用doPost()之外,IO资源处理不正确,每行都有自己的try / catch,异常被抑制,并且信息写入stdout,InputStream#available()被误解, DataOutputStream被用于没有明确原因,InputStream从未被关闭,等等。不,那肯定不是那样的。请参阅basic Java IObasic Java Exception教程,详细了解如何正确使用它们。这是一个小小的重写servlet应该是什么样子:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
    File file = new File("/path/to/all/movies", filename);

    response.setHeader("Content-Type", "video/quicktime");
    response.setHeader("Content-Length", file.length());
    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(new FileInputStream(file));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
        if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
    }
}

将其映射到web.xml,如下所示:

<servlet>
    <servlet-name>movieServlet</servlet-name>
    <servlet-class>com.example.MovieServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>movieServlet</servlet-name>
    <url-pattern>/movies/*</url-pattern>        
</servlet-mapping>

上述JavaScript示例准确显示了您应该如何使用它。只需使用路径/movies并在其后添加文件名,如/movies/filename.movrequest.getPathInfo()将返回/filename.mov

答案 1 :(得分:1)

<EMBED SRC="<your servlet hosting the movie>" WIDTH=100 HEIGHT = 196 AUTOPLAY=true CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/">

答案 2 :(得分:0)

最广泛支持的方法是嵌入Flash播放器(swf)并从程序中返回FLV文件。 Flash将自动流式传输flv文件。

http://snipplr.com/view/288/flash-video-player-html-code/