【发布时间】:2017-10-22 22:32:57
【问题描述】:
大家好,我尝试在 jsp 页面中播放视频并获得成功。我如何在jsp中播放多个视频,视频文件在特定文件夹中(文件系统) 下面是显示一个视频文件的代码
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>
**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>
动作类
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;
}
}
我这样改变了我的 jsp
<html>
<body>
<form action="getFiles">
<input type="submit" value="check">
</form>
<s:iterator value="filesList">
<video width="400" controls>
<source src="<s:url value='getFiles.action?fileName='/><s:property/>" type="video/mp4" />
<source src="mov_bbb.ogg" type="video/ogg"> Your browser does not support HTML5 video. </video>
</s:iterator>
</body>
</html>
struts.xml
<struts>
<package name="user" namespace="/" extends="struts-default">
<action name="getFiles" class="com.pradeep.videostream.VideoStreamingAction">
<result name="success">index.jsp</result>
</action>
<!-- <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 static final long serialVersionUID = 1L;
private InputStream inputStream;
private String yourContentType;
private File[] filesArray = null;
private List<String> filesList = new ArrayList<String>();
//getters and setters
public String execute() throws Exception {
File folder = new File("D://svn videos");
filesArray = folder.listFiles();
for (int i = 0; i < filesArray.length; i++) {
String fileName=filesArray[i].getName();
filesList.add(fileName);
}
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String fileName = request.getParameter("fileName");
System.out.println("GGGGGGGGGGGGGGGGG "+fileName);
File file = new File(fileName);
setInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)));
return SUCCESS;
}
}
【问题讨论】: