从网页中的 servlet 读取 Quicktime 电影?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,它会触发
GET
请求,但 servlet 仅侦听POST
请求。您需要在doGet()
方法而不是doPost()
中完成此任务。您还需要指示网络浏览器您到底要发送哪些信息。这是通过 HTTP
Content-Type 来完成的
标头。您可以在此处找到最常用的内容类型(mime 类型)的概述。您可以使用 < code>HttpServletResponse#setContentType() 来设置它。对于 Quicktime.mov
文件,内容类型应为video/quicktime
。此外,每种媒体格式都有其自己的使用
和/或
也就是说,所发布的 servlet 代码老实说写得很糟糕。除了错误地使用了 doPost() 之外,IO 资源处理也不正确,每一行都有自己的 try/catch,异常被抑制,不良信息被写入 stdout,InputStream# available() 被误解,
DataOutputStream
被无缘无故地使用,InputStream
从未被关闭,等等。不,这当然不是这样的。请参阅基本 Java IO 和 基本 Java 异常 教程,了解有关正确使用它们的更多信息。下面稍微重写了 servlet 的外观:将其映射到
web.xml
中,如下所示:前面发布的 JavaScript 示例准确地显示了您应该如何使用它。只需使用路径
/movies
并在其后附加文件名,就像/movies/filename.mov
一样。request.getPathInfo()
将返回/filename.mov
。To start, it is firing a
GET
request, but the servlet is listening onPOST
requests only. You need to do this task in thedoGet()
method rather thandoPost()
.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 useHttpServletResponse#setContentType()
to set it. In case of Quicktime.mov
files, the content type ought to bevideo/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.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, theDataOutputStream
is been used for no clear reason, theInputStream
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:Map it in
web.xml
as follows: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
. Therequest.getPathInfo()
will return/filename.mov
.最广泛支持的方法是嵌入 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/