如何使用struts2和jsp

时间:2017-05-05 07:01:08

标签: jsp video struts2

您正在努力使用struts2从系统位置播放jsp文件中的视频文件。但是,如果我将视频文件(Sample.mp4)放在eclipse中的web-content下,只需使用jsp中的视频标签和fileName,就像下面一样,它将得到播放。

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

如何播放系统位置示例中的视频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>

1 个答案:

答案 0 :(得分:0)

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

<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 action

<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>
动作类中的

是来自文件系统的视频文件并正在进行流式传输

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;
        }
}