简体   繁体   English

RequestDispatcher.forward到媒体文件?

[英]RequestDispatcher.forward to a media file?

I recently had an issue to resolve and found a solution, but that solution could potentially be greatly simplified if I could somehow use RequestDispatcher.forward to forward a request to a media URL. 我最近有一个问题要解决并找到了解决方案,但如果我能以某种方式使用RequestDispatcher.forward将请求转发到媒体URL,那么该解决方案可能会大大简化。

From the docs, it says that it only supports servlet, JSP file, or HTML file . 从文档中可以看出它只支持servlet,JSP文件或HTML文件 I tried with a media URL anyway and it did not complain, however it's not returning the correct headers (eg mime type) and perhaps there or other faulty things but anyway it did not worked as expected. 无论如何我尝试使用媒体网址并且没有抱怨,但它没有返回正确的标题(例如mime类型),也许还有其他或其他错误的东西,但无论如何它没有按预期工作。

Is there a way I could use RequestDispatcher.forward with a media URL so that the response is served exactly as if the media URL was requested to the web server directly? 有没有办法可以将RequestDispatcher.forward与媒体URL一起使用,以便提供响应,就像直接向Web服务器请求媒体URL一样?

EDIT: 编辑:

I can attest that at least the Content-Type is correct, since it's returning Content-Type text/html; charset=iso-8859-1 我可以证明至少Content-Type是正确的,因为它返回Content-Type text/html; charset=iso-8859-1 Content-Type text/html; charset=iso-8859-1 for any media file. Content-Type text/html; charset=iso-8859-1适用于任何媒体文件。 Also, I have tried opening the url directly in Window Media Player and it seems to be downloading the entire video before starting to play, which is unacceptable. 此外,我已经尝试直接在Window Media Player中打开网址,它似乎是在开始播放之前下载整个视频,这是不可接受的。 Therefore I can safely assume that forward doesn't just hand back the control to IIS or at least does it incorrectly for media files. 因此,我可以放心地假设forward不只是将控件交还给IIS,或者至少对于媒体文件不正确。

The solution you found points to the right direction, but I have no knowledge of coldfusion , but with servlets the same thing can be done very easily. 您找到的解决方案指向正确的方向,但我不了解coldfusion ,但使用servlet可以很容易地完成同样的事情。

If you want to use RequestDispatcher.forward method then you can specify a servlet url in the forward method. 如果要使用RequestDispatcher.forward方法,则可以在forward方法中指定servlet url。 And in that servlet all you need to do is read the media and send it using OutputStream as the response. 在该servlet中,您需要做的就是读取媒体并使用OutputStream作为响应发送它。

For example: 例如:

In your servlets doGet or doPost method you just need to set the content type according to your media, read it and send it. 在servlet的doGetdoPost方法中,您只需根据媒体设置内容类型,阅读并发送。

Below is a simple example you can use to send the media as response from the servlet: 下面是一个简单的示例,您可以使用该示例将介质作为来自servlet的响应发送:

public void doGet(HttpServletRequest request, HttpServletResponse response)  {
    response.setContentType("video/x-ms-wmx");
    ServletContext ctx = getServletContext();

    InputStream is = ctx.getResourceAsStream("/path/to/media/file");

    int read = 0;
    byte bytes[] = new byte[1024];

    OutputStream os = response.getOutputStream();
    while((read = is.read(bytes)) != -1) {
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM