RequestDispatcher.forward到媒体文件? [英] RequestDispatcher.forward to a media file?

查看:131
本文介绍了RequestDispatcher.forward到媒体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近有 问题要解决 ,并找到一个解决方案,但如果我能以某种方式使用 RequestDispatcher.forward 将请求转发到媒体网址,该解决方案可能会大大简化。



从文档中,它说它只支持 servlet,JSP文件或HTML文件。我尝试了一个媒体网址,无论如何,它没有抱怨,但它不返回正确的标题(例如mime类型),也许有或其他错误的东西,但无论如何没有按预期工作。



有一种方法,我可以使用 RequestDispatcher.forward 与媒体网址,以便响应的提供完全一样,如果请求媒体网址到网络服务器?



编辑



可以证明至少 Content-Type 是正确的,因为它返回 Content-Type text / html; charset = iso-8859-1 任何媒体文件。此外,我已经尝试直接在Window Media Player中打开网址,它似乎是在开始播放之前下载整个视频,这是不能接受的。因此,我可以安全地假设 forward 不会将控制权交回IIS,或至少对媒体文件不正确。

coldfusion ,但是servlet



如果你想使用 RequestDispatcher.forward 方法,你可以指定一个servlet url中的forward方法。
在那个servlet中,所有你需要做的是读取媒体并使用 OutputStream 作为响应发送它。



例如:



在您的servlet doGet doPost 方法
您只需根据您的媒体设置内容类型,读取它并发送。



下面是一个简单的例子使用从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();
}


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.

From the docs, it says that it only supports servlet, JSP file, or HTML file. I tried with a media URL anyway and it did not complain, however it's not returning the correct headers (e.g. mime type) and perhaps there or other faulty things but anyway it did not worked as expected.

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?

EDIT:

I can attest that at least the Content-Type is correct, since it's returning Content-Type text/html; charset=iso-8859-1 for any media file. 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. 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.

解决方案

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.

If you want to use RequestDispatcher.forward method then you can specify a servlet url in the forward method. And in that servlet all you need to do is read the media and send it using OutputStream as the response.

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.

Below is a simple example you can use to send the media as response from the 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();
}

这篇关于RequestDispatcher.forward到媒体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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