您好,我正在努力使用 struts2 从系统位置播放 jsp 文件中的视频文件。但是,如果我将视频文件(Sample.mp4)放在 eclipse 中的 web-content 下,并且只需在 jsp 中使用带有文件名的视频标签(如下所示),它就会播放。
<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>
in 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页面
<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 操作
<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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)