如何使用Struts2和JSP从系统位置播放视频 [英] How do i play video from system location using struts2 and jsp

查看:231
本文介绍了如何使用Struts2和JSP从系统位置播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用struts2从系统位置播放jsp文件中的视频文件.但是,如果我将视频文件(Sample.mp4)放在eclipse的web-content下,然后在jsp中将video标签与fileName一起使用,如下所示,它将可以播放.

Hi am struggling to play video file in jsp file from system location using struts2. But if i place video file(Sample.mp4) under web-content in eclipse and just use the video tag in jsp with fileName like below it will get play.

<source src="Sample.mp4" type="video/mp4"/>

如何播放系统位置示例d:/video/sample.mp4中的视频?

How do i play the video which is there in system location example d:/video/sample.mp4 ?

动作类

public class DownloadAction extends ActionSupport {

    private InputStream fileInputStream;
    private String fileToDownload = "D://video//Sample.mp4";
    private String fileName;

    private String contentType = "video/mp4";


    public String execute() throws Exception {
        fileInputStream = new FileInputStream(fileToDownload);
        return SUCCESS;
    }
    public InputStream getFileInputStream() {
        return fileInputStream;
    }
    public void setFileInputStream(InputStream fileInputStream) {
        this.fileInputStream = fileInputStream;
    }
    public String getFileToDownload() {
        return fileToDownload;
    }
    public void setFileToDownload(String fileToDownload) {
        this.fileToDownload = fileToDownload;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getContentType() {
        return contentType;
    }
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

struts.xml

<action name="download" class="com.sample.actions.DownloadAction">
   <result name="success" type="stream">
    <param name="contentType">${contentType}</param>
    <param name="inputName">fileInputStream</param>
    <param name="contentDisposition">attachment;filename="${fileName}"</param>
    <param name="bufferSize">1024</param>
   </result>
  </action>

在Jsp中

<body>
 <%
  String url = request.getScheme() + "://" + request.getServerName()
    + ":" + request.getServerPort() + request.getContextPath();
  url = url + "//download";
 %>
 <video width="320" height="240" controls>
  <source src=<%=url%>>
 </video>
</body>

推荐答案

这是用于显示视频的jsp页面

Here is an jsp page to for video display

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <video width="400" controls>
 <source src="<s:url value="videoStream" />" type="video/mp4"/>
  <source src="mov_bbb.ogg" type="video/ogg">
  Your browser does not support HTML5 video.
</video>
</body>
</html>

将s:url请求映射到struts.xml中的struts action操作

Map the s:url request to struts action action in struts.xml

<struts>
    <package name="user" namespace="/" extends="struts-default">
        <action name="videoStream" class="com.pradeep.videostream.VideoStreamingAction">
            <result name="success" type="stream">
                <param name="contentType">${yourContentType}</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${yourFileName}"</param>
                <param name="bufferSize">1024</param>
            </result>
        </action>
    </package>
</struts>

在动作类中,正在处理来自文件系统的视频文件并进行流式传输

in the action class am fething video file from file system and doing streaming

public class VideoStreamingAction extends ActionSupport {
    private InputStream inputStream;
     private String yourContentType;

        // getters and setters 

 public String execute() throws Exception {
           yourContentType = "video/mp4";
           File file =new File("D://svn videos//Create Java Spring Web MVC Project With Maven [EDITED].mp4");
           setInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)));   
           return SUCCESS;
        }
}

这篇关于如何使用Struts2和JSP从系统位置播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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