Struts2 - 将请求重定向到另一个应用程序 - 视频

Struts2 - redirect request to another application - video

我有一个 struts2 应用程序,我需要在其中显示另一个网络应用程序中正在显示的视频。

这是显示视频的代码。此 IP 无法在互联网上访问,只能在我的 struts 应用程序所在的服务器上访问。

<img src="http://<ip_from_other_server/showVideo">

我需要在 struts2 中执行一个操作,我可以发出请求并将其转发到其他服务器的响应。可能吗?

除了 struts 解决方案之外,您还可以尝试设置一个 (apache) 代理,它将请求重定向到您的视频服务器。有了它,您就没有那么庞大的软件堆栈。例子在这里:Apache mod_proxy

但是如果您决定使用 struts 解决方案,这里有一些想法:

  • 创建一个可以连接到您的视频服务器的操作,可能使用如下答案的 FileOutputStream:Download file with java
  • 使用那个文件(临时保存在某处),为它打开一个流并在你的 <img href="mypicture">-Action 中返回它,也许就像这里:Struts2 ServletResponseAware or here Struts Stream Result

如果你愿意,我可以再详细一点。

我可以找到解决办法。

我用过这个项目:https://github.com/mitre/HTTP-Proxy-Servlet

有了它,我可以将请求重定向到其他服务器。在客户端视图中,我自己的服务器正在响应请求。

在 web.xml 中,我输入了以下内容:

<servlet>
    <servlet-name>otherServer</servlet-name>
    <servlet-class>org.mitre.dsmiley.httpproxy.URITemplateProxyServlet</servlet-class>
    <init-param>
        <param-name>targetUri</param-name>
        <param-value>http://{_ipOtherServer}:{_portOtherServer}/myAction</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>otherServer</servlet-name>
    <url-pattern>/otherServer/action/*</url-pattern>
</servlet-mapping>

此外,在 struts.xml 中,我不得不排除所有匹配 /otherServer 的请求。

<constant name="struts.action.excludePattern" value="/otherServer/.*"/>