在网页上从servlet中读取quicktime电影? [英] Read quicktime movie from servlet in a webpage?

查看:91
本文介绍了在网页上从servlet中读取quicktime电影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

以下是我在servlet中的代码:

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

这里是html页面,如Ryan所示:

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?

推荐答案

首先,它发射一个 GET servlet只监听 POST 请求。您需要在 doGet()方法中执行此任务,而不是 doPost()

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().

您还需要指示浏览器正在发送什么信息。这是通过HTTP Content-Type 标题。您可以在这里找到 最常用内容类型(MIME类型)的概述。您可以使用 HttpServletResponse#setContentType() 来设置它。对于Quicktime .mov 文件,内容类型应该是 video / quicktime

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");

此外,每种媒体格式都有自己的嵌入方式,使用< ;嵌入> 和/或< object> 元素。您需要查阅媒体格式供应商的文档以了解如何使用它。对于Quicktime .mov 文件,您需要查阅 Apple 。仔细阅读这份文件。它写得很好,它也处理crossbrowser inconintenties。您可能更愿意 Do这是简单的方法 在简单的JavaScript的帮助下可以透明地桥接crossbrowser inconintenties。
$ b

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>






也就是说,发布的servlet代码老实说可怕的书面。除了 doPost()错误地被使用之外,IO资源处理是不正确的,每一行都有它自己的try / catch,异常被抑制,差信息被写入stdout , InputStream#available()被误解了, DataOutputStream 没有明确的原因, InputStream 永远不会关闭,等等。不,这当然不是这样。请参阅基本Java IO 基本Java异常教程,以了解有关正确使用它们的更多信息。这里稍微重写一下servlet应该如何看起来像:


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

映射它 web.xml 如下: $ b

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>

上述JavaScript示例显示了您应该如何使用它。只需使用路径 / movies ,然后像 /movies/filename.mov 那样附加文件名。 request.getPathInfo()将返回 /filename.mov

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.

这篇关于在网页上从servlet中读取quicktime电影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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