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

发布于 2024-09-04 02:57:14 字数 1658 浏览 8 评论 0原文

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

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

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

这是我在 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/">

有什么想法吗?

I have a servlet that construct response to a media file request by reading the file from server:

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

Then write that stream to the response stream. My question is how do I play the media file in the webpage using embed or object tag to read the media stream from the response?

Here is my code in the 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) {
    }
}

And here is html page as Ryan suggested:

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街道布景 2024-09-11 02:57:14

首先,它会触发 GET 请求,但 servlet 仅侦听 POST 请求。您需要在 doGet() 方法而不是 doPost() 中完成此任务。

您还需要指示网络浏览器您到底要发送哪些信息。这是通过 HTTP Content-Type 来完成的 标头。您可以在此处找到最常用的内容类型(mime 类型)的概述。您可以使用 < code>HttpServletResponse#setContentType() 来设置它。对于 Quicktime .mov 文件,内容类型应为 video/quicktime

response.setContentType("video/quicktime");

此外,每种媒体格式都有其自己的使用 和/或 元素嵌入的方式。您需要查阅媒体格式供应商的文档以了解如何使用它的详细信息。对于 Quicktime .mov 文件,您需要查阅 苹果。请仔细阅读本文档。它写得很好,并且还可以处理跨浏览器的不一致问题。您可能更愿意它是一种简单的方法,借助简单的 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 从未被关闭,等等。不,这当然不是这样的。请参阅基本 Java IO基本 Java 异常 教程,了解有关正确使用它们的更多信息。下面稍微重写了 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.mov 一样。 request.getPathInfo() 将返回 /filename.mov

To start, it is firing a GET request, but the servlet is listening on POST requests only. You need to do this task in the doGet() method rather than doPost().

You also need to instruct the webbrowser what information exactly you're sending. This is to be done with the HTTP Content-Type header. You can find here an overview of the most used content types (mime types). You can use HttpServletResponse#setContentType() to set it. In case of Quicktime .mov files, the content type ought to be video/quicktime.

response.setContentType("video/quicktime");

Further, every media format has its own way of being embedded using the <embed> and/or the <object> element. You need to consult the documentation of the media format vendor for details how to use it. In case of Quicktime .mov files, you need to consult Apple. Carefully read this document. It is well written and it handles crossbrowser inconsitenties as well. You would likely prefer to Do It the Easy Way with help of a simple JavaScript to bridge crossbrowser inconsitenties transparently.

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

That said, the posted servlet code is honestly said terrible written. Apart from the doPost() incorrectly been used, the IO resource handling is incorrect, every line has its own try/catch, exceptions are been suppressed and poor information is written to stdout, InputStream#available() is been misunderstood, the DataOutputStream is been used for no clear reason, the InputStream is never been closed, etcetera. No, that is certainly not the way. Please consult the basic Java IO and basic Java Exception tutorials to learn more about using them properly. Here's a slight rewrite how the servlet ought to look like:

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) {}
    }
}

Map it in web.xml as follows:

<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>

The aforeposted JavaScript example shows exactly how you should use it. Just use path /movies and append the filename thereafter like so /movies/filename.mov. The request.getPathInfo() will return /filename.mov.

吃颗糖壮壮胆 2024-09-11 02:57:14
<EMBED SRC="<your servlet hosting the movie>" WIDTH=100 HEIGHT = 196 AUTOPLAY=true CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/">
<EMBED SRC="<your servlet hosting the movie>" WIDTH=100 HEIGHT = 196 AUTOPLAY=true CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/">
你如我软肋 2024-09-11 02:57:14

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

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

The most widely supported way would be to embed a flash player (swf) and return a FLV file from your program. Flash will automatically stream the flv file.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文